C# DataTable 通过Linq分组

datatable我们是经常使用到的,但是需要对数据进行分组,具体代码如下:

var result = dt.AsEnumerable().GroupBy(f => new
{
    type = f.Field<string>("type"),
    value = f.Field<string>("value"),
    site_id = f.Field<string>("site_id")
}).Select(f => new
{
    type = f.Key.type,
    value = f.Key.value,
    site_id = f.Key.site_id,
    detect_time = f.Min(x => x.Field<long>("detect_time")),
    data_type = f.Min(x => x.Field<string>("data_type"))
}).ToList();

这样就实现了分组,返回的result相当于一个dynamic对象。很简单的

猜你喜欢

转载自www.cnblogs.com/duanjt/p/9088249.html