1
votes

With EF 6, I was querying like this and it was working nice.

IQueryable<Student> query = _testHelper.buildQuerty(id, userId)
        .Include(x => x.Class)
        .Include(x => x.Subjects)
        .Include(x => x.Subjects.Select(y => y.Category));

Problem: The same does not work in EF Core 2.0.

Error

System.ArgumentException occurred HResult=0x80070057 Message=The property expression 'Subjects => {from Subjects y in Subjects select [y].Category}' is not valid. The expression should represent a property access: 't => t.MyProperty'. For more information on including related data, see http://go.microsoft.com/fwlink/?LinkID=746393.

Opened up link, and refactored like this but it still not working and gives same error.

List<Student> query = _testHelper.buildQuerty(id, userId)
        .Include(x => x.Class)
        .Include(x => x.Subjects)
        .ThenInclude(Subjects => Subjects.Select(y => y.Category)).tolist();

Where is the problem?

1

1 Answers

6
votes

You're using the last chain of ThenInclude wrongly. You should use it like the code below.

List<Student> query = _testHelper.buildQuerty(id, userId)
        .Include(x => x.Class)
        .Include(x => x.Subjects)
        .ThenInclude(subject => subject.Category);

With this ThenInclude extension method, you're dealing with an instance of Subject because of the last of use of Include which deal with a collection of that type.