Solved

Aggregating data from a list using LINQ in C#

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now