0

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?

1
  • Post the class that is created by CreateReport201Data. We can't even tell if ReportDate is a string or a DateTime, etc. Commented May 30, 2018 at 19:32

2 Answers 2

3
 var report201FinalList = (from report201Objects in listReports201
        group report201Objects by  report201Objects.ReportDate into grouping
        orderby grouping.Key
        select new Report201
        {
             ReportDate = grouping.Key,
             PurchaseDec = grouping.Sum(g => g.PurchaseDesc),
             QuasiCashDec = grouping.Sum(g => g.QuasiCashDec ),
             RepId = string.Join(",", grouping.Select(g => g.RepId)),
             ...etc... 
        }).ToList();

In this query, grouping is an IGrouping<>, an IEnumerable with a Key property.

Sign up to request clarification or add additional context in comments.

9 Comments

Thank you for the tip. But how would I handle a string value in the same fashion? Example: RepID = grouping.Select(g => g.RepID), since each object may have different RepID value. That example won't work, but the idea stays the same
Not clear what you want. The grouping could/should hold multiple different RepIDs. See my comment about posting the class. As an outlinme at least.
I mean, each object will have IDs, and I'm trying to use grouping to get RepID for each object. I need to assign RepID to new objects created by the grouping logic: ` RepID = grouping.Select(id => id.RepID)`
You still haven't clarified the 1-N relation.
I'm not sure what you mean by clarifying. Each object in report201 list has a field called RepID. It is not a number, so using a Sum when grouping will not work. I need to assign that field from each object to a RepID when adding to a new list
|
2

Your initial linq seems right!

The bug is at the first snippet, the second lineCnt++; is wrong and skips a line. Remove that and use the first linq you wrote. lineCnt=1 also might be wrong as Henk notices first.

for(int lineCnt = 0; lineCnt < fileLines.Count(); lineCnt++)
{
    if (fileLines[lineCnt].Contains("REPORT-201"))
    {
        report201 = new Report201();
        report201= CreateReport201Data(lineCnt, fileLines);
        listReports201.Add(report201);
        //lineCnt++;
    }
}

1 Comment

Yes, nice catch. Also worth adding that it should be lineCnt = 1; lineCnt <= or lineCnt = 0; lineCnt <

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.