Solved

Aggregating data from a list using LINQ in C#

Posted on 2014-10-24
5
440 Views
Last Modified: 2016-02-15
I have a list (list<>) of detail objects that I need to create another list of header objects with.
This detail list contains the following information:

Order No
Line Item
Line Item Extended Amount
Customer Name
Customer Ship to Address
Customer Ship to City/State/Zip

I want to create a list of header Information containing the following:
Order No
Customer Name
Customer Ship to Address
Customer Ship to City/State/Zip
Order Total
Line Item Count

I would think it's possible in LINQ, but I'm not as versed in LINQ as I probably should be.
Any ideas/suggestions are appreciated!
0
Comment
Question by:Euless_Tech
  • 3
  • 2
5 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 40402639
You can add others to the group by and key, but this is should get you going:

 class tester
    {
        public string orderNo;
        public string CustomerName;
        public string ShipTo;
        public int LineItem;
        public float total;
    }


            List<tester> list = new List<tester>();

            var summary =
            list.GroupBy(l => new {l.CustomerName, l.ShipTo, l.orderNo})
                .Select (g => 
                    new {
                           CustomerName = g.Key.CustomerName,
                           OrderNo = g.Key.orderNo,
                           ShipTo = g.Key.ShipTo,
                           Total = g.Sum(x => x.total),
                           LineCount = g.Count()
                    }
                    );

Open in new window

0
 

Author Comment

by:Euless_Tech
ID: 40403096
Hey Kyle,
Thanks for responding so quickly. I'm ready to award the points to you. I have another question: Is it possible to interpret a flag in a row and place the result in the resulting LINQ results? For instance, if one of the items in the order detail from the example above was back ordered, I would like to place a flag in the resulting LINQ results for the header that indicates that one or more items are back ordered. Is that possible?
0
 

Author Closing Comment

by:Euless_Tech
ID: 40403098
Thanks for the quick reply! Was easier than I thought it would be. I guess when you know what you're doing, anything's easy.
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 40403246
Just to respond for anyone else coming across your second question, you can use the count for that:

//short hand for if Count > 0 then true else false;
 BackOrderFlag = g.Count( x=> x.BackOrdered == true) > 0 ? true : false;
0
 

Author Comment

by:Euless_Tech
ID: 40403265
Thanks Kyle! I appreciate the help.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question