开发者

How do I do a "group by" in my .net DataTable

开发者 https://www.devze.com 2022-12-20 06:08 出处:网络
I would like to 开发者_StackOverflowimplement a sql style group by for the columns in my datatabe is this possible?

I would like to 开发者_StackOverflowimplement a sql style group by for the columns in my datatabe is this possible?

c#, .net 2.0


Use the DataTableExtensions extension methods to convert the DataTable to an IEnumerable<DataRow>, then use the IEnumerable<T>.GroupBy() extension.

DataTable tbl = ..

var q = tbl.AsEnumerable().GroupBy( r => r.Field<string>("Company") )
                          .Select( g => new { Company = g.Key, TotalSales = g.Sum( s => s.Field<decimal>("Sales") ) } );
0

精彩评论

暂无评论...
验证码 换一张
取 消