1.linq List<object> group by 累加
var nameVals = (from x in modelParts
group x by x.PartName into g
select new NameVal
{
name = g.Key,
val = g.Aggregate("", (y, z) => y + "(" + z.ParaName + "=" + z.ParaVal + ")")
});
数据原型:
组合后数据:
2.linq datatable group by 多个字段
var modelParasList = (from dd in dtList.AsEnumerable()
group dd by new { mid = dd.Field<int>("id"), mname = dd.Field<string>("name") } into g
select new ModelParaModel
{
ID = g.Key.mid,
Name = g.Key.mname,
ParaItems =( from gg in g
select new ModelParaItem
{
T1 = gg.Field<int>("AA"),
T2 = gg.Field<int>("BB"),
T3 = gg.Field<string>("CC"),
T4 = gg.Field<string>("DD")
}
).ToList()
}).ToList();
分组前数据:
处理后数据: