How to select multiple values after using Max() in

2019-07-05 02:01发布

I have the following LINQ query:

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);

I want to select both the object id and price of the object with the maximum price. How can I do that?

1条回答
我只想做你的唯一
2楼-- · 2019-07-05 02:32

Use an order by descending clause and call 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();
查看更多
登录 后发表回答