我正在对一些 C# 3 集合过滤器进行原型设计并遇到了这个问题。我有一系列产品:
public class MyProduct
{
public string Name { get; set; }
public Double Price { get; set; }
public string Description { get; set; }
}
var MyProducts = new List<MyProduct>
{
new MyProduct
{
Name = "Surfboard",
Price = 144.99,
Description = "Most important thing you will ever own."
},
new MyProduct
{
Name = "Leash",
Price = 29.28,
Description = "Keep important things close to you."
}
,
new MyProduct
{
Name = "Sun Screen",
Price = 15.88,
Description = "1000 SPF! Who Could ask for more?"
}
};
现在,如果我使用 LINQ 进行过滤,它会按预期工作:
var d = (from mp in MyProducts
where mp.Price < 50d
select mp);
如果我将 Where 扩展方法与 Lambda 结合使用,过滤器也可以工作:
var f = MyProducts.Where(mp => mp.Price < 50d).ToList();
问题:有什么区别,为什么使用一个而不是另一个?