Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 包含路径表达式必须引用在类型上定义的导航属性。_C#_Entity Framework_Linq_Eager Loading - Fatal编程技术网

C# 包含路径表达式必须引用在类型上定义的导航属性。

C# 包含路径表达式必须引用在类型上定义的导航属性。,c#,entity-framework,linq,eager-loading,C#,Entity Framework,Linq,Eager Loading,我的linq查询 model.Questions = db.Questions .Where (x => x.CategoriesID == categoryId) .Include (qc => qc.QuestionCounters.Where(x => x.MemberID == User.Identity.GetUserId())) .In

我的linq查询

   model.Questions = db.Questions
                     .Where (x => x.CategoriesID == categoryId)
                     .Include (qc => qc.QuestionCounters.Where(x => x.MemberID == User.Identity.GetUserId()))
                     .Include (qf => qf.QuestionFavorites.Where(x => x.MemberId == User.Identity.GetUserId()))
                     .Include (qt => qt.QuestionTags)
                     .ToList();
产生错误

'包含路径表达式必须引用导航属性 在类型上定义。使用虚线路径进行参考导航 属性和集合导航的选择运算符 财产。”

知道为什么会这样吗?

好的。结果是

 IQueryable<HomeViewModel> test = db.Questions
                                  .Where(x => x.CategoriesID == categoryId)
                                  .Select(q => q.ToHomeViewModel(User.Identity.GetUserId()));
毕竟,需求如何包含


感谢您在@jle上发表评论

正如一些人评论的那样,您不能在Include中使用
Where
方法

免责声明:我是项目的所有者

EF+查询IncludeFilter功能允许筛选相关实体

model.Questions = db.Questions
                 .Where (x => x.CategoriesID == categoryId)
                 .IncludeFiler (qc => qc.QuestionCounters.Where(x => x.MemberID == User.Identity.GetUserId()))
                 .IncludeFiler (qf => qf.QuestionFavorites.Where(x => x.MemberId == User.Identity.GetUserId()))
                 .IncludeFiler (qt => qt.QuestionTags)
                 .ToList();
维基:

解决方案#2

另一种技术是使用投影(这是我的库在引擎盖下所做的)


不能使用“包含”来选择数据。这方面已经有很多帖子了。@Equalsk还有其他选择吗?这个让我抓狂重复-看这个和这个。你的include表达式中不能有“Where”。
model.Questions = db.Questions
                 .Where (x => x.CategoriesID == categoryId)
                 .IncludeFiler (qc => qc.QuestionCounters.Where(x => x.MemberID == User.Identity.GetUserId()))
                 .IncludeFiler (qf => qf.QuestionFavorites.Where(x => x.MemberId == User.Identity.GetUserId()))
                 .IncludeFiler (qt => qt.QuestionTags)
                 .ToList();
bd.Questions
     .Select(q = new {
        Question = q,
        QuestionCounters = q.QuestionCounters.Where(x => x.MemberID == memberId),
        QuestionFavorites = q.QuestionFavorites.Where(x => x.MemberId == memberId),
        QuestionTags = q.QuestionTags
     })
     .ToList()
     .Select(x => x.Question)
     .ToList();