我有这个简单的查询,我希望有一个类别列表,并为每个类别提供内部产品的数量。
from category in categories
join product in products
on category.Id equals product.Category_Id into productsPerCategories
select new Categories
{
Category = category,
products = productsPerCategories.Count()
};
但如果没有产品,我不希望该类别出现。使用我当前的构造,将会有内部没有产品的类别。我怎样才能做到这一点?
答案 0 :(得分:1)
只需添加条件
where productsPerCategories.Any()
或
where productsPerCategories.Count() > 0
所以你的查询是:
from category in categories
join product in products on category.Id equals product.Category_Id into productsPerCategories
where productsPerCategories.Any()
select new Categories
{
Category = category,
products = productsPerCategories.Count()
};