如何在LINQ中选择属于分组的字段?
例如:
var result = from a in table
where Date >= DataStart && Date <= DataStop
group a by a.Field2 into b
select new
{
AA = b.Sum(x => x.Total),
BB = a.Field2
};
答案 0 :(得分:5)
由于您按Field2
分组,Field2
是该群组的关键,因此您可以使用b.Key
var result = from a in table
where a.Date >= DataStart && a.Date <= DataStop
group a by a.Field2 into b
select new
{
AA = b.Sum(x => x.Total),
BB = b.Key
};