How can you retrieve the products that are contained in a list of id’s ?
var ids = new List() { 1, 2, 3, 500 };
using (var context = new AdventureWorksEntities())
{
var products = from product in context.Products
where ids.Contains(product.ProductID)
select product;
WriteProducts(products);
}
The good news, this is supported in Entity Framework v4.0, but since I’m on 3.5 I needed to find another solution. Luckily using the info from my previous post I can write an expression for this:
var ids = new List() { 1, 2, 3, 500 };
using (var context = new AdventureWorksEntities())
{
Expression> idMatching = null;
foreach (var id in ids)
{
int productId = id;
if(idMatching == null)
{
idMatching = x => x.ProductID == productId;
}
else
{
idMatching = idMatching.Or(x => x.ProductID == productId);
}
}
var products = context.Products.Where(idMatching);
WriteProducts(products);
}