LINQ DataTable Query - Group Aggregation

My colleague asked me this question. Instead of doing the conventional way, he liked to try on LINQ for simplicity. The naming convention was ignored for this example. This was something that we hooked up together.

var  query = from  row in  dtInvoice.AsEnumerable()
    group  row by   new  { InvNo =  row.Field< string >( "InvNo" ),
    EmpUNID = row.Field< decimal >( "EmployeeUNID" )} into  grp                        
    orderby  grp.Key.InvNo
        select new
        {                           
            Key =  grp.Key,  
            InvNo = grp.Key.InvNo,
            EmpID = grp.Key.EmpUNID,                                                           
            TotalCost = grp.Sum(r => r.Field< decimal >( "TotalCost" )),
            TotalRev = grp.Sum(r => r.Field< decimal >( "TotalRev" ))
        }; 

In order to come out with this solution, you have to understand the concept of LINQ which has similarities to T-SQL.

Explanations:

dtIncoice is a datatable. You can use generic list or any preferred choice. Instead of doing this grouping in T-SQL Stored Procedure, my colleague chose to do this in C# instead.

You notice that when we perform grouping, we have to do this:

group row by new { }

Do that if you plan to have multiple grouping, if not you can do this instead:

group row by { row.Field<string>("InvNo") }

Then why do I have to do this in multiple grouping?

group row by new { A = some values, B = some values }

If you don't do that, you will probably get an error when compiling your code or won't get proper results:

Anonymous type members must be declared with a member assignment, simple name or member access.

However, you can ignore that if you have single field

In order to use InvNo and EmpUNID from grouping aggregation, you need to reassign that values when you perform a Select in the LINQ query. That makes a lot of sense.

select new

{

InvNo = grp.Key.InvNo,
EmpID = grp.Key.EmpUNID, 

}

If not, when you query this later for further processing, you are not able to access it. Hope you find this useful.

Thank you.

Published Friday, May 16, 2008 1:21 AM by wenching
Filed under:

Comments

# re: LINQ DataTable Query - Group Aggregation

Friday, July 18, 2008 10:44 AM by Thanks

This has helped a lot. I used this for uniqueness:

           DataTable dt = GetDataTable();

           var items = (from row in dt.AsEnumerable()

                       group row by new {ID = row.Field<double>("Unique_ID")}

                       into grp

                           select new

                           {

                               ID=grp.Key.ID,

                               RowCount=grp.Count()

                           }).Where(e=>e.RowCount>1);

           if(items.Count()>0)

               return false;

           else

               return true;

# re: LINQ DataTable Query - Group Aggregation

Wednesday, December 03, 2008 5:38 AM by banu_pushpa

I would like to know how to use the LIKE operator for this Linq data table

# re: LINQ DataTable Query - Group Aggregation

Thursday, June 04, 2009 4:51 AM by Nikola

Help a lot :) thnx

# re: LINQ DataTable Query - Group Aggregation

Tuesday, November 24, 2009 10:47 AM by Tim

Very helpful, thankyou!

# re: LINQ DataTable Query - Group Aggregation

Monday, September 13, 2010 4:13 PM by skin256

Thank you, excellent and concise example.

# re: LINQ DataTable Query - Group Aggregation

Friday, November 12, 2010 10:11 AM by SparTodd

Thanks for posting this example!  I helped me out tremendously.  It would be nice if the results could be inserted into an existing DataTable in a DataSet, but at least I was able to figure out how to iterate over the results to accomplish that.

Leave a Comment

(required) 
(required) 
(optional)
(required)