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") ) } );
精彩评论