?
Solved

Aggregating data from a list using LINQ in C#

Posted on 2014-10-24
5
Medium Priority
?
466 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 2000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

765 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