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
Solved

Help with LINQ group by

Posted on 2014-01-24
1
368 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 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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!
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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