我有以下LINQ查询:
var query =
(from p in obj1
group p by p.objID into g
let totalSum = g.Sum(p => p.ObjPrice)
select new { MyObjectID = g.Key, totalSum })
.Max(g => g.totalSum);
我想以最高价格选择对象的对象ID和价格。我怎么能这样做?
答案 0 :(得分:7)
按降序子句使用订单并致电FirstOrDefault()
。
(from p in obj1
group p by p.objID into g
let totalSum = g.Sum(p => p.ObjPrice)
orderby totalSum descending
select new { MyObjectID = g.Key, totalSum }).FirstOrDefault();