Sunt prototipul unor filtre de colectare C# 3 și am dat peste asta.
Am o colecție de produse:
public class MyProduct
{
public string Name { get; set; }
public Double Price { get; set; }
public string Description { get; set; }
}
var MyProducts = new List
{
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?"
}
};
Acum, dacă folosesc LINQ pentru filtrarea funcționează așa cum era de așteptat:
var d = (from mp in MyProducts
where mp.Price < 50d
select mp);
Și dacă folosesc metoda extensiei Where în combinație cu un filtru Lambda funcționează și filtrul:
var f = MyProducts.Where(mp => mp.Price < 50d).ToList();
Question: What is the difference, and why use one over the other?