How to select multiple values โ€‹โ€‹after using Max () in LINQ for objects?

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 identifier of the object and the price of the object with the maximum price. How can i do this?

+3
source share
1 answer

Use descending order 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(); 
+7
source

Source: https://habr.com/ru/post/918697/


All Articles