I have a one to many relationship between a student table and a courses table. I wanted to return all students that have courses that match a certain criteria at the moment I have this
.Context.Users.Where(it =>
it.Category.Description == "Student" &&
it.Courses.All(p => p.Submitted == true && p.StatusId == null) &&
it.Courses.Count > 0);
Now this works fine; if I remove “it.Courses.Count > 0 ” then I get matching Students that have no courses. Is there a better way of doing this without the Count?
Thanks,
Dale
You could improve it a little bit by replacing
it.Courses.Count > 0bywhich asks if the collection has “any” element (at least one) and which translates to
EXISTSin SQL.