1、C# 3或更高版本使用匿名类型
// 声明式查询语法var result3 = from x in table group x by new { x.Column1, x.Column2 } into g select new { g.Key.Column1, g.Key.Column2, QuantitySum = g.Sum(x => x.Quantity) };// /或方法的语法var result4 = table.GroupBy(x => new { x.Column1, x.Column2 }) .Select(g => new { g.Key.Column1, g.Key.Column2 , QuantitySum= g.Sum(x => x.Quantity) });
2、C# 7.1或更高版本使用元组和推断的元组元素名
// 声明式查询语法var result = from x in table group x by (x.Column1, x.Column2) into g select (g.Key.Column1, g.Key.Column2, QuantitySum: g.Sum(x => x.Quantity));//或方法的语法var result2 = table.GroupBy(x => (x.Column1, x.Column2)) .Select(g => (g.Key.Column1, g.Key.Column2, QuantitySum: g.Sum(x => x.Quantity)));