Solved

Aggregating data from a list using LINQ in C#

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…

832 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