我使用以下表达式:
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)
它工作正常,但我需要过滤不活跃的价格层。
有任何想法吗?
Update
这是我的解决方案:
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
如果没有投影或单独的查询来加载导航属性,则无法在单个查询中执行此操作。 Lambda 表达式为Include
仅接受点分符号作为参考,Select
对于集合,但您不能进行任何过滤。
使用 DbContext API 时,可以在单独的查询中进行过滤:
var data = context.Entry(product)
.Collection(p => p.ChildProducts)
.Query()
// Here are your filters
.Load();
Edit:
投影需要类似:
var data = context.Products
.Where(...)
.Select(p => new
{
Product = p,
ChildProducts = p.ChildProducts.Where(...)
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)