Fluent Api Include Where Clause

本文关键字:Clause Where Include Api Fluent | 更新日期: 2023-09-27 18:18:09

我需要有封面图片的产品。但是当我添加pic => pic.IsCover时,它会抛出异常。否则没有问题。我怎样才能修好它?

错误:

Include路径表达式必须引用在该类型上定义的导航属性。对引用导航属性使用点点路径,对集合导航属性使用Select操作符。参数adyi: path

Thanks to all

_db.ProdSmartSorts
    .Where(x => catIds.Contains((int)x.Product.CategoryId))
    .OrderBy(x => x.ProdSmartId)
    .Select(x => x.Product)
    .Include(p => p.Pictures.Where(pic => pic.IsCover))
    .Skip(prodCount * (pageNumber - 1))
    .Take(prodCount)
    .ToList();

Fluent Api Include Where Clause

Entity Framework不支持使用Include方法进行过滤。你可以在这里投票:https://entityframework.codeplex.com/workitem/47

是否在DbSet工作后直接放置。include()方法调用?例如

_db.ProdSmartSorts
 .Include(p => p.Pictures)
 .Where(x => catIds.Contains((int)x.Product.CategoryId))
 .OrderBy(x => x.ProdSmartId)
 .Select(x => x.Product)
 .Skip(prodCount * (pageNumber - 1))
 .Take(prodCount)
 .ToList();

我认为Include方法只适用于dbContext中的DbSet对象。如果你试着把它放到链的更下面,根据具体情况,你只有IQueryable或IEnumerable对象可用的方法。

另外,据我所知,你不能使用Include函数来过滤,就像你尝试的那样。因此,您必须加载所有与图片实体相关的图像。

EDIT:对不起-刚刚意识到这个问题是专门要求在Include()方法上进行过滤。请忽视。