gpt4 book ai didi

c# - 查找名称为LINQ的最低价格返回的产品

转载 作者:太空宇宙 更新时间:2023-11-03 18:08:09 25 4
gpt4 key购买 nike

我是LINQ的新手,我试图在列表中找到最低的价格并返回名称。

我一直在搜索,找不到任何可以使用的东西。

该列表位于类Category中,但是我必须在main中写出结果。

它是Microsoft Visual Studio中的C#。

我必须从中找到最低价格的列表是这样的:

public static IEnumerable<Product> GetProducts( )
{
List<Product> products = new List<Product>( );
products.Add( new Product { Name = "Milk", Price = 90, CategoryID = 4, ID = 1 } );
products.Add( new Product { Name = "Cheese", Price = 130, CategoryID = 4, ID = 2 } );
products.Add( new Product { Name = "Butter", Price = 110, CategoryID = 4, ID = 3 } );

products.Add( new Product { Name = "Apple juice", Price = 230, CategoryID = 1, ID = 4 } );
products.Add( new Product { Name = "Grape juice", Price = 240, CategoryID = 1, ID = 5 } );
products.Add( new Product { Name = "Beetroot juice", Price = 300, CategoryID = 1, ID = 6 } );
products.Add( new Product { Name = "Carrot juice", Price = 190, CategoryID = 1, ID = 7 } );
products.Add( new Product { Name = "Ginger ale", Price = 990, CategoryID = 1, ID = 8 } );

products.Add( new Product { Name = "Oregano", Price = 500, CategoryID = 2, ID = 9 } );
products.Add( new Product { Name = "Salt", Price = 550, CategoryID = 2, ID = 10 } );
products.Add( new Product { Name = "Pepper", Price = 490, CategoryID = 2, ID = 11 } );

products.Add( new Product { Name = "Carrots", Price = 300, CategoryID = 3, ID = 12 } );
products.Add( new Product { Name = "Spinach", Price = 250, CategoryID = 3, ID = 13 } );
products.Add( new Product { Name = "Onion", Price = 200, CategoryID = 3, ID = 14 } );
products.Add( new Product { Name = "Garlic", Price = 150, CategoryID = 3, ID = 15 } );
products.Add( new Product { Name = "Tomatoes", Price = 100, CategoryID = 3, ID = 16 } );

return products;
}

最佳答案

from p in Products where p.Price == Products.Min(x=>x.Price) select p.Name


First列表中获取 Ordered的问题在于,它无法处理多个商品具有相同最低价格的可能性。

关于c# - 查找名称为LINQ的最低价格返回的产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22288924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com