3

My linq query

   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();

produces the error

'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.'

Any ideas why is this happening?

OrElse
  • 9,709
  • 39
  • 140
  • 253
  • You can't use Include to select data. There are lots of posts on this already. – Equalsk Aug 03 '17 at 14:19
  • @Equalsk Are there any alternatives? This one freaks me out – OrElse Aug 03 '17 at 14:21
  • Duplicate - See this https://stackoverflow.com/questions/15980665/ef-lambda-the-include-path-expression-must-refer-to-a-navigation-property and this https://stackoverflow.com/questions/38676029/the-include-path-expression-must-refer-to-a-navigation-property-defined-on-the-t. –  Aug 03 '17 at 14:21
  • 1
    You can't have "Where" in your include expression. – jle Aug 03 '17 at 14:32

2 Answers2

2

As some people commented, you cannot use Where method in Include.

Disclaimer: I'm the owner of the project Entity Framework Plus

EF+ Query IncludeFilter feature allow filtering related entities.

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();

Wiki: EF+ Query IncludeFilter

Solution #2

Another technique is by using projection (which is what my library do under the hood)

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();
Jonathan Magnan
  • 10,874
  • 2
  • 38
  • 60
0

Ok. Ended up with

 IQueryable<HomeViewModel> test = db.Questions
                                  .Where(x => x.CategoriesID == categoryId)
                                  .Select(q => q.ToHomeViewModel(User.Identity.GetUserId()));

and

public static HomeViewModel ToHomeViewModel(this Question q, string memberId)
{
    return new HomeViewModel()
    {
        QuestionCounters = q.QuestionCounters.Where(x => x.MemberID == memberId),
        QuestionFavorites = q.QuestionFavorites.Where(x => x.MemberId == memberId),
        QuestionTags = q.QuestionTags
    };
}

How needs include after all? ;)

Thanks for commenting @jle

OrElse
  • 9,709
  • 39
  • 140
  • 253