use LINQ to find the product with the cheapest value?

前端 未结 3 1876
梦谈多话
梦谈多话 2021-01-13 15:27

Im learning LINQ and I want to find the cheapest product from the following list:

List products = new List { 
                n         


        
3条回答
  •  不思量自难忘°
    2021-01-13 16:27

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

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题