LINQ to Entity - 使用Lambda表达式的Include

3

我有一个小问题,不太清楚该如何解决。在下面的示例中,我想选择一组包含已激活ProductItems的ProductCategories列表。

public IEnumerable<ProductCategory> ListProductCategories()
        {
            return _entities.ProductCategorySet.Include("ProductItems").Where(x => x.ProductItems.Active == true).ToList();               
        }

问题是我无法在lambda表达式中访问productItem属性Active,这是什么问题?当我尝试编写类似上面的linq查询时,我是否完全想错了?

1个回答

6

可能会有多个项目。您可能希望选择所有项目都处于活动状态的类别:all

return _entities.ProductCategorySet
                .Include("ProductItems")
                .Where(x => x.ProductItems.All(item => item.Active))
                .ToList();

该解决方案并没有真正解决问题。您的解决方案仅返回具有任何产品项的产品类别,无论它们是否处于活动状态。我想要返回的是所有产品类别和产品项,只有当产品项处于active == true状态时才返回。 - Webking

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接