I was looking to find the solution to my issue, but could not find exactly what I need, so I decided to create this question:
I have a list that contains objects. Those object are created based on the data from the file. The file will have about 30 reports. Based on the report name, in that case it is "REPORT-201", I will have 28 objects in the list with only 2 of them with the dates needed:
'4/26/2018 12:00:00 AM'
'4/27/2018 12:00:00 AM'
Other 26 objects with the date '1/1/0001 12:00:00 AM', I do not need
How can I create another list that will contain objects with totals for the dates I need. I guess, I need to somehow create a logic to group the objects by the dates, summarize all the fields and add each object into another list
I have the following logic that created a preliminary list containing 28 objects:
for(int lineCnt = 1; lineCnt <. fileLines.Count(); lineCnt++)
{
if (fileLines[lineCnt].Contains("REPORT-201"))
{
report201 = new Report201();
report201= CreateReport201Data(lineCnt, fileLines);
listReports201.Add(report201);
lineCnt++;
}
}
I tried to summarize the data the following way:
report201Purchase = listReports201.Where(report201=> report201.ReportDate == reportDate).Sum(report201 => report201.PurchaseDec);
But that gives me only the data for the object with the last report date. However, I need to have a list with as many dates as file has with totals for the fields in each object
Now, I'm trying to create a list that would contain only objects with specific dates with totals for those dates:
var report201FinalList = (from report201Objects in listReports201
group report201Objects by new { report201Objects.ReportDate } into grouping
orderby grouping.Key.ReportDate
select new Report201
{
ReportDate = grouping.Key.ReportDate,
PurchaseDec = ?
QuasiCashDec = ?
...etc...
}).ToList();
How can I summarize the field in the objects and add those objects into another list or if there is another way to handle what I need?