Entity framework 4 实体框架4.1-选择
我使用的表达方式如下:Entity framework 4 实体框架4.1-选择,entity-framework-4,lambda,Entity Framework 4,Lambda,我使用的表达方式如下: ProductRepository.Query.Include(Function(x) x.ChildProducts.Select(Function(y) y.PriceTiers.Where(Function(z) z.IsActive))).Where(Function(x) x.Categories.Any(Function(y) y.ID = ID)) 得到这个错误: The Include path expression must refer to a na
ProductRepository.Query.Include(Function(x) x.ChildProducts.Select(Function(y) y.PriceTiers.Where(Function(z) z.IsActive))).Where(Function(x) x.Categories.Any(Function(y) y.ID = ID))
得到这个错误:
The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.
如果我删除此项:
.Where(Function(z) z.IsActive)
它工作正常,但我需要过滤不活动的价格层
有什么想法吗
更新
以下是我的解决方案:
Public Function GetProductsByCategoryID(ID As Integer) As System.Collections.Generic.IEnumerable(Of Core.Entities.Product) Implements Core.Interfaces.IProductService.GetProductsByCategoryID
Dim Col = ProductRepository.Query.Where(Function(x) x.Display AndAlso x.Categories.Any(Function(y) y.ID = ID)) _
.Select(Function(x) New With { _
.Product = x,
.ChildProducts = x.ChildProducts.Where(Function(y) y.Display).Select(Function(y) New With { _
.ChildProduct = y,
.PriceTiers = y.PriceTiers.Where(Function(z) z.IsActive)
})
}).ToList.Select(Function(x) x.Product)
Return Col
End Function
如果没有投影或单独的查询来加载导航属性,则无法在单个查询中执行此操作。
Include
的Lambda表达式对于引用只接受点符号,对于集合只接受Select
,但不能进行任何筛选
使用DbContext API时,可以在单独的查询中进行筛选:
var data = context.Entry(product)
.Collection(p => p.ChildProducts)
.Query()
// Here are your filters
.Load();
编辑:
投影需要如下内容:
var data = context.Products
.Where(...)
.Select(p => new
{
Product = p,
ChildProducts = p.ChildProducts.Where(...)
});
@Lad-所以这假设您有一个单一的产品可以操作,对吗?如果是这样的话,你将如何在一系列产品中做到这一点。您必须改为使用带投影的自定义查询。@Lad-您能给我指一个带投影的自定义查询的示例吗?我添加了投影示例。是的,您必须创建DTO,因为投影不可能映射到实体,并且在同一类型中,您不能筛选已加载的子对象。