假设存在适当的关联属性,您可以执行以下操作:
var activeCategories = from category in _db.Categories
where category.Items.Any(item => item.IsActive)
select category;
如果您必须加入,请尝试以下操作:
var activeCategories = from category in _db.Categories
join item in _db.Items on category.Id equals item.CategoryId
into categoryGroup
where categoryGroup.Any(i => i.IsActive)
select category;
(或者)
var activeCategories = from category in _db.Categories
join item in _db.Items.Where(i => i.IsActive)
on category.Id equals item.CategoryId
into categoryGroup
where categoryGroup.Any()
select category;
(或者)
var activeCategories = from category in _db.Categories
where _db.Items
.Where(i => i.IsActive)
.Select(i => i.CategoryId)
.Contains(category.Id)
select category;