?
Solved

Help with LINQ group by

Posted on 2014-01-24
1
Medium Priority
?
378 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 2000 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

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…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

764 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