When I do the query:
Users.OfType<Student>().Where(u => u.StudentClasses.Any(sc => sc.Class.TermId == 1 && sc.Class.SubFormId == 1))
I get nothing. But this query returns true:
StudentClasses.Any(sc => sc.Class.TermId == 1 && sc.Class.SubFormId == 1)
So basically this is the same as:
Users.OfType<Student>().Where(u => true)
Which returns all the students. So OfType<Student> works.
I’m 100% sure that there are students in these classes StudentClasses but for some reason I get back no student although the subquery returns true.
Student is inherited from User, but only Student has StudendClass. What would be wrong with this query?
I basically want students in a particular subform/term (all these properties are in a Class.
I’m using Table per heirachy. Model builder:
modelBuilder.Entity<User>()
.Map<Teacher>(m => m.Requires("UserType").HasValue("Teach"))
.Map<Student>(m => m.Requires("UserType").HasValue("Stu"))
.Map<Staff>(m => m.Requires("UserType").HasValue("Staff"));
Edit:
This also returns nothing:
Users.OfType<Student>().Where(u => u.StudentClasses.Any(sc => true))
I am 99% sure there aren’t. (1% for the potential case you have discovered some serious EF bug.) If there are students in classes with
Class.TermId == 1andClass.SubFormId == 1your query must return those students.Your test query…
…has nothing to do with your original query. It only says that there is at least one row in the
StudentClassestable withClass.TermId == 1andClass.SubFormId == 1.It doesn’t prove that you have any student which is in this particular class (or in these particular classes if there is more than one class with
Class.TermId == 1andClass.SubFormId == 1). All your students could be in the other classes and your original query would correctly return no student as result. So, this is by no means identical with(context.)Users.OfType<Student>().Where(u => true).If you are still 100% sure you should show the content of your tables in a small example to make the problem reproducable for us.