开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜