本文介绍了如何在LINQ to Objects中使用Max()之后选择多个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下LINQ查询:

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



我要选择这两个对象ID,并与最高价格的目标价。我该怎么做?

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

推荐答案

按降序条款,并呼吁使用命令 FirstOrDefault()

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

这篇关于如何在LINQ to Objects中使用Max()之后选择多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 09:52