Solved

Help with LINQ group by

Posted on 2014-01-24
1
359 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
1 Comment
 
LVL 62

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

21 Experts available now in Live!

Get 1:1 Help Now