I have two class.
public class Category
{
public int Id {get;set;}
public string Name{get;set;}
}
public class Product
{
public int ProductId{get;set;}
public string Name{get;set;}
public List<Category> ProductCategory {get;set;}
}
Now i have Category list based on that i need to get product
List<string> filtercategory ={"HomeMade","Uncooked"}.
I need to all Products which doesn't belong to 'HomeMade' and 'Uncooked'.
What should be linq query logic on this?
thanks.
You can simply filter with Where
:
List<Product> productList = GetProducts();
var filteredProducts = productList
.Where(p => p.ProductCategory
.All(pc => !filtercategory.Contains(pc.Name)))
.ToList();
This filters the productList
and takes only those products for which all category names are not contained in filtercategory
.
I only tested this in linq-to-sql, not with entities, so I'm not 100% sure that this also works for entities.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments