use LINQ to find the product with the cheapest value?

前端 未结 3 1868
梦谈多话
梦谈多话 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:22

    You need to order by the price first and then select the first.

    if(products.Any()){
        var productWithCheapestPrice = products.OrderBy(p => p.Price).First();
    }
    

提交回复
热议问题