How to select multiple values after using Max() in LINQ to Objects?

给你一囗甜甜゛ 提交于 2019-12-10 19:08: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:


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


来源:https://stackoverflow.com/questions/5399719/how-to-select-multiple-values-after-using-max-in-linq-to-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!