Solved

Help with LINQ group by

Posted on 2014-01-24
1
373 Views
Last Modified: 2016-02-10
Hi.
I have the following statement which returns totals by SSN. I need to add another column to the result.  How do I do that?  I want to include the LASTNAME.  Any ideas?

   var query = from rawdata in myTable.AsEnumerable()
                         group rawdata by rawdata.Field<string>("SSN") into grp
                         orderby grp.Key
                         select new
                         {
                             SSN = grp.Key,
                             TOTAL = grp.Sum(r => r.Field<decimal>("SALES"))
                         
                       // WANT TO ADD LASTNAME HERE!
                       // LASTNAME IS A FIELD IN myTable

                           LASTNAME = ????????????????




                         };
0
Comment
Question by:JElster
[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
1 Comment
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39806724
Hi  JElster;

This should give you the wanted results.

var query = from rawdata in myTable.AsEnumerable()
            group rawdata by rawdata.Field<string>("SSN") into grp
            orderby grp.Key
            select new
            {
                SSN = grp.Key,
                TOTAL = grp.Sum(r => r.Field<decimal>("SALES")),
                LASTNAME = grp.Select(n => n.Field<string>("LASTNAME")).FirstOrDefault()
            };

Open in new window

0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

689 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