gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:17:51 26 4
gpt4 key购买 nike

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

最佳答案

使用 order by descending 子句并调用 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();

关于c# - 在 LINQ to Objects 中使用 Max() 后如何选择多个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5399719/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com