Im learning LINQ and I want to find the cheapest product from the following list:
List products = new List {
n
-
Alternatively, you could simply order them and take the first result, this is assuming you're after the Product
object and not the Price
value; like so.
var cheapestProduct = products.OrderBy(p => p.Price).FirstOrDefault();
var mostExpensiveProduct = products.OrderByDescending(p => p.Price).FirstOrDefault();
- 热议问题