I combined 2 lists in 1 list with some columns and I'd like to order it by Ascending. The column that I want to order is HODD.
I tried to read the others answers about this problem, but I don't know how I can apply them to my code.
This is my code:
var data1 = File.ReadAllLines("read" + campionatoselezTxt.Text + "stats.txt").ToList();
var data2 = File.ReadAllLines("read" + campionatoselezTxt.Text + "bex.txt").ToList();
var dataList1 = new List<Data1>();
foreach (var data in data1)
{
var columns = data.Split(';');
dataList1.Add(new Data1
{
Campionato = columns[0],
Data = columns[1],
Home = columns[2],
Away = columns[3],
HSFT = int.Parse(columns[4]),
ASFT = int.Parse(columns[5]),
HSHT = int.Parse(columns[6]),
ASHT = int.Parse(columns[7]),
HSSH = int.Parse(columns[8]),
ASSH = int.Parse(columns[9])
//other int properties
});
}
var dataList2 = new List<Data2>();
foreach (var data in data2)
{
var columns = data.Split(';');
dataList2.Add(new Data2
{
Home = columns[0],
Away = columns[1],
HODD = columns[4],
XODD = columns[5],
AODD = columns[6],
});
}
var combinedDataList = from d1 in dataList1
//join d2 in dataList2 on d1.Home equals d2.Home
join d2 in dataList2 on new { d1.Home, d1.Away } equals new { d2.Home, d2.Away }
select new CombinedData
{
Campionato = d1.Campionato,
Data = d1.Data,
Home = d2.Home,
Away = d2.Away,
HSFT = d1.HSFT,
ASFT = d1.ASFT,
HSHT = d1.HSHT,
ASHT = d1.ASHT,
HSSH = d1.HSSH,
ASSH = d1.ASSH,
HODD = d2.HODD,
XODD = d2.XODD,
AODD = d2.AODD,
RisFin = d2.RisFin,
Over05SH = d2.Over05SH
};
var combinedDataList = (from d1 in dataList1
//join d2 in dataList2 on d1.Home equals d2.Home
join d2 in dataList2 on new { d1.Home, d1.Away } equals new { d2.Home, d2.Away }
select new CombinedData
{
Campionato = d1.Campionato,
Data = d1.Data,
Home = d2.Home,
Away = d2.Away,
HSFT = d1.HSFT,
ASFT = d1.ASFT,
HSHT = d1.HSHT,
ASHT = d1.ASHT,
HSSH = d1.HSSH,
ASSH = d1.ASSH,
HODD = d2.HODD,
XODD = d2.XODD,
AODD = d2.AODD,
RisFin = d2.RisFin,
Over05SH = d2.Over05SH
}).OrderBy(p=>p.HODD);
please try
Related
I have two groups like below, theyh have different data. Based on both I need to create an xml file .
How can I write a for-loop for both groups and generate a single xml file?
var groups = checkFile.AsEnumerable().GroupBy(x => new { DocNum = x.Field<int>("orderid"), Type = x.Field<string>("Type"), ProdName = x.Field<string>("ProdName"), Status = x.Field<string>("Status"), productno = x.Field<string>("productno"), uom = x.Field<string>("uom"), customer = x.Field<string>("customer"), remark = x.Field<string>("remark"), U_JobNumber = x.Field<string>("U_JobNumber"), U_SalesPerson = x.Field<string>("U_SalesPerson"), U_POnum = x.Field<string>("U_POnum"), U_JobType = x.Field<string>("U_JobType"), PlannedQty = x.Field<decimal>("PlannedQty"), OriginNum = x.Field<int?>("OriginNum"), orderdate = x.Field<DateTime>("orderdate"), duedate = x.Field<DateTime>("duedate"), DocTotal = x.Field<decimal>("DocTotal") });
var groups2 = checkFile2.AsEnumerable().GroupBy(x => new { DocNum = x.Field<int>("DocNum") });
//now i need to take both group data inside this loop to print the file
foreach (var group in groups)
{
var stringwriter = new StringWriter();
using (var xmlWriter = XmlWriter.Create(stringwriter, new XmlWriterSettings { Indent = true }))
{
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("Root");
xmlWriter.WriteEndElement();
}
var xml = stringwriter.ToString();
XmlDocument docSave = new XmlDocument();
docSave.LoadXml(stringwriter.ToString());
docSave.Save(System.IO.Path.Combine(#SystemSettings.ImportBankStatementPendingFolderPath, "DocNum -" + group.Key.DocNum + ".xml"));
count++;
}
Try following :
DataTable checkFile = new DataTable();
var groups = checkFile.AsEnumerable().GroupBy(x => new
{
DocNum = x.Field<int>("orderid"),
Type = x.Field<string>("Type"),
ProdName = x.Field<string>("ProdName"),
Status = x.Field<string>("Status"),
productno = x.Field<string>("productno"),
uom = x.Field<string>("uom"),
customer = x.Field<string>("customer"),
remark = x.Field<string>("remark"),
U_JobNumber = x.Field<string>("U_JobNumber"),
U_SalesPerson = x.Field<string>("U_SalesPerson"),
U_POnum = x.Field<string>("U_POnum"),
U_JobType = x.Field<string>("U_JobType"),
PlannedQty = x.Field<decimal>("PlannedQty"),
OriginNum = x.Field<int?>("OriginNum"),
orderdate = x.Field<DateTime>("orderdate"),
duedate = x.Field<DateTime>("duedate"),
DocTotal = x.Field<decimal>("DocTotal")
});
DataTable checkFile2 = new DataTable();
//now i need to take both group data inside this loop to print the file
foreach (var group in groups)
{
List<DataRow> groups2 = checkFile2.AsEnumerable().Where(x => group.Key.DocNum == x.Field<int>("DocNum")).ToList();
}
In a list i have 4 rows and I am try to get all the rows of the list but it is giving only one row, how to get all the rows of the list.
I have tried below code
public async Task<ResponseUserModel> get()
{
List<ResponseUserModel> responseUsers = new List<ResponseUserModel>();
using (nae2sasqld0003Entities context = new nae2sasqld0003Entities())
{
var listt = context.Producers.Select(all => all).ToList();
foreach (var item in listt)
{
responseUsers.Add(new ResponseUserModel
{
ProducerName = item.ProducerName,
ResidentState = item.ResidentState,
ResidentCity = item.ResidentCity,
ProducerStatus = item.ProducerStatus,
ProducerCode = item.ProducerCode,
MasterCode = item.MasterCode,
NationalCode = item.NationalCode,
LegacyChubbCodes = item.LegacyChubbCodes,
LegacyPMSCode = item.LegacyPMSCode,
ProducingBranchCode = item.ProducingBranchCode,
CategoryCode = item.CategoryCode
});
}
return responseUsers;
}
}
please let me know where i to fix the issue
Use list to return all:
List<ResponseUserModel> responseUsers = new List<ResponseUserModel>();
then
foreach (var item in listt)
{
responseUsers.Add(new ResponseUserModel
{
ProducerName = item.ProducerName,
ResidentState = item.ResidentState,
ResidentCity = item.ResidentCity,
ProducerStatus = item.ProducerStatus,
ProducerCode = item.ProducerCode,
MasterCode = item.MasterCode,
NationalCode = item.NationalCode,
LegacyChubbCodes = item.LegacyChubbCodes,
LegacyPMSCode = item.LegacyPMSCode,
ProducingBranchCode = item.ProducingBranchCode,
CategoryCode = item.CategoryCode
});
}
return responseUsers;
Note: change return type of the method to IList<ResponseUserModel>
or in this way
using (var context = new nae2sasqld0003Entities())
{
return context.Producers.Select(item =>
new ResponseUserModel
{
ProducerName = item.ProducerName,
ResidentState = item.ResidentState,
ResidentCity = item.ResidentCity,
ProducerStatus = item.ProducerStatus,
ProducerCode = item.ProducerCode,
MasterCode = item.MasterCode,
NationalCode = item.NationalCode,
LegacyChubbCodes = item.LegacyChubbCodes,
LegacyPMSCode = item.LegacyPMSCode,
ProducingBranchCode = item.ProducingBranchCode,
CategoryCode = item.CategoryCode
}).ToList();
}
I'm trying to return two lists to my view, these lists help me fill some webgrid, I searched the web and I can not find a solution, I'm new to this
My Controller
public ActionResult Index()
{
List<WebGrid> list = new List<WebGrid>();
using (Web_INCAEntities dc = new Web_INCAEntities())
{
var v = (from a in dc.Cat_Proyecto
join b in dc.Cat_Pais on a.Id_Pais equals b.ID
join c in dc.Cat_estado on a.Id_Estado equals c.Id
select new WebGrid
{
ID = a.ID,
ID_kEY = a.ID_kEY,
Cliente = a.Cliente,
Tipo_servicio = a.Tipo_servicio,
Descripcion = a.Descripcion,
Contratista = a.Contratista,
INCA_PM = a.INCA_PM,
Importe_INCA = a.Importe_INCA,
Importe_Cliente = a.Importe_Cliente,
calle = a.calle,
colonia = a.colonia,
Estado = c.Estado,
Pais = b.Pais
});
list = v.ToList();
}
List<WebGrid> list_Usuario = new List<WebGrid>();
using (Web_INCAEntities dc = new Web_INCAEntities())
{
var v = (from a in dc.Usuarios
select new WebGrid
{
Usuario = a.Usuario,
nombres = a.Nombres,
apellidos = a.Apellido_Paterno,
empresa = a.Area_Empresa,
estatus_Usuario = a.Estatus,
alcance = a.Id_Alcance
});
list_Usuario = v.ToList();
}
return View("../Admin/Administrador", list, list_Usuario);
}
in the return View tried to return to the view and my two lists to fill two web grid, only that I get an error, but I want to know how together these two lists so I can fill my grid
As both the list are of same type i.e List<WebGrid>
you can use AddRange and concat both the list and pass to view
public ActionResult Index()
{
List<WebGrid> list = new List<WebGrid>();
using (Web_INCAEntities dc = new Web_INCAEntities())
{
var v = (from a in dc.Cat_Proyecto
join b in dc.Cat_Pais on a.Id_Pais equals b.ID
join c in dc.Cat_estado on a.Id_Estado equals c.Id
select new WebGrid
{
ID = a.ID,
ID_kEY = a.ID_kEY,
Cliente = a.Cliente,
Tipo_servicio = a.Tipo_servicio,
Descripcion = a.Descripcion,
Contratista = a.Contratista,
INCA_PM = a.INCA_PM,
Importe_INCA = a.Importe_INCA,
Importe_Cliente = a.Importe_Cliente,
calle = a.calle,
colonia = a.colonia,
Estado = c.Estado,
Pais = b.Pais
});
list = v.ToList();
}
List<WebGrid> list_Usuario = new List<WebGrid>();
using (Web_INCAEntities dc = new Web_INCAEntities())
{
var v = (from a in dc.Usuarios
select new WebGrid
{
Usuario = a.Usuario,
nombres = a.Nombres,
apellidos = a.Apellido_Paterno,
empresa = a.Area_Empresa,
estatus_Usuario = a.Estatus,
alcance = a.Id_Alcance
});
list_Usuario = v.ToList();
}
list.AddRange(list_Usuario);
return View("../Admin/Administrador", list);
}
or else you can create a class and use it
public class WebGridModel
{
public List<WebGrid> List1{get;set;}
public List<WebGrid> List2{get;set;}
}
and use it like this
public ActionResult Index()
{
List<WebGrid> list = new List<WebGrid>();
using (Web_INCAEntities dc = new Web_INCAEntities())
{
var v = (from a in dc.Cat_Proyecto
join b in dc.Cat_Pais on a.Id_Pais equals b.ID
join c in dc.Cat_estado on a.Id_Estado equals c.Id
select new WebGrid
{
ID = a.ID,
ID_kEY = a.ID_kEY,
Cliente = a.Cliente,
Tipo_servicio = a.Tipo_servicio,
Descripcion = a.Descripcion,
Contratista = a.Contratista,
INCA_PM = a.INCA_PM,
Importe_INCA = a.Importe_INCA,
Importe_Cliente = a.Importe_Cliente,
calle = a.calle,
colonia = a.colonia,
Estado = c.Estado,
Pais = b.Pais
});
list = v.ToList();
}
List<WebGrid> list_Usuario = new List<WebGrid>();
using (Web_INCAEntities dc = new Web_INCAEntities())
{
var v = (from a in dc.Usuarios
select new WebGrid
{
Usuario = a.Usuario,
nombres = a.Nombres,
apellidos = a.Apellido_Paterno,
empresa = a.Area_Empresa,
estatus_Usuario = a.Estatus,
alcance = a.Id_Alcance
});
list_Usuario = v.ToList();
}
var returnObj = new WebGridModel
{
List1= list;
List2=list_Usuario ;
}
return View("../Admin/Administrador", returnObj );
}
I have a page where user can select any number of search filters to apply search
When user clicks on search, these parameters are passed to my GetIndicatorData method to perform the query. However, it doesn't seem to work for me.
Here is my code
public static List<tblindicators_data_custom> GetIndicatorsData(string status, int service_id, int operator_id, string year, string frequency)
{
var date = Convert.ToDateTime(year + "-01-01");
int[] numbers = status.Split(',').Select(n => int.Parse(n)).ToArray();
var ict = new ICT_indicatorsEntities();
var result = from ind in ict.tblindicators_data
join ser in ict.tblservices on ind.service_id equals ser.Id
join oper in ict.tbloperators on ind.operator_id equals oper.Id
where numbers.Contains(ind.status) && (ind.date_added.Year == date.Year)
select new
{
ind.Id,
ind.service_id,
ind.survey_id,
ind.operator_id,
ind.type,
ind.date_added,
ind.quater_start,
ind.quater_end,
ind.status,
ind.month,
service = ser.name,
operator_name = oper.name
};
List<tblindicators_data_custom> data = new List<tblindicators_data_custom>();
foreach (var item in result)
{
tblindicators_data_custom row = new tblindicators_data_custom();
row.Id = item.Id;
row.survey_id = item.survey_id;
row.service_id = item.service_id;
row.service_name = item.service;
row.operator_id = item.operator_id;
row.operator_name = item.operator_name;
row.date_added = item.date_added;
row.quater_start = item.quater_start;
row.type = item.type;
row.quater_end = item.quater_end;
row.month = item.month == null? DateTime.Now:item.month;
row.status = item.status;
data.Add(row);
}
return data;
}
Is there a more elegant/concise way of this; I'd like to get rid of foreach loop with the WorkListItem initialization code.
var queryable = registrations.Select(
r => new
{
r.Id, r.AccountNumber, r.DateAdded, r.DateUpdated, r.Patient, r.Patient.InsuranceInfos
});
var list = queryable.ToList();
var workListItems = new List<WorkListItem>();
foreach (var anonymous in list)
{
var w = new WorkListItem
{
Id = anonymous.Id,
ClientAccountId = anonymous.AccountNumber,
DateAdded = anonymous.DateAdded,
DateUpdated = anonymous.DateUpdated,
Patient = anonymous.Patient,
InsuraceInfos = anonymous.Patient.InsuranceInfos
};
workListItems.Add(w);
}
return workListItems;
Yes you can completely cut out the "middle-man" as it were and select straight into a new WorkListItem as below:
var list = registrations.Select(r => new WorkListItem
{
Id = r.Id,
ClientAccountId = r.AccountNumber,
DateAdded = r.DateAdded,
DateUpdated = r.DateUpdated,
Patient = r.Patient,
InsuraceInfos = r.Patient.InsuranceInfos
}).ToList();