我正在尝试编写一个查询来从组中只找到一项,它们按行业和重量分组,然后从这个我必须得到重量最大和平衡最大的地方这是示例:
var data = new[] {
new {ID = 1, Industry = 2, Weight = 2, Balance = 500},
new {ID = 2, Industry = 2, Weight = 2, Balance = 300},
new {ID = 3, Industry = 2, Weight = 1, Balance = 100},
new {ID = 5, Industry = 4, Weight = 1, Balance = 100},
new {ID = 6, Industry = 4, Weight = 2, Balance = 150},
new {ID = 7, Industry = 4, Weight = 1, Balance = 300},
};
var res = from a in data group a by new {a.Industry, a.Weight} into g
let ID = g.First().ID
let Balance = g.Max(a => a.Balance)
select new { ID, g.Key.Industry, g.Key.Weight, Balance};
Console.WriteLine(res);
所以结果我应该只得到两条记录
ID Industry Weight Balance
1 2 2 500
6 4 2 150
但是通过上面的查询,我得到了 4 条记录有什么建议吗?
问候, 德米特里