在LINQ to Objects中使用Max()后如何选择多个值?

时间:2011-03-23 01:00:15

标签: c# linq linq-to-objects

我有以下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和价格。我怎么能这样做?

1 个答案:

答案 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();