Solved

Aggregating data from a list using LINQ in C#

Posted on 2014-10-24
5
442 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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…

685 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