我使用以下表达式:
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
答案 0 :(得分:3)
如果没有投影或单独的查询来加载导航属性,则无法在单个查询中执行此操作。 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(...)
});