If I understand correctly in your initial query you want to get all Grade
names that are related to SchoolProducts
matching certain criteria. The names should be sorted.
If this is the case you should be able to re-write your query like this:
var q2 = context.Grades
.Where(g => context.SchoolProducts
.Any(sp => sp.SchoolCode == School.Code && sp.Gender.Name == genderName &&
sp.GradeId = g.Id)
// join to Grades
.OrderBy(g => g.SortOrder).ThenBy(g => g.Name)
.Select(c => g.Name);
names are not unique you will need again the .Distinct()
call to remove the duplicates, but the way the query is expressed you will not get repetitions due to joining with the SchoolProducts
The behavior that you have described is the design decision taken when a .Where()
clause will be executed in-memory. For projections and post-projection operations we have decided to silently fall back to split execution.
Telerik by Progress
Check out the latest announcement
about Telerik Data Access vNext as a powerful framework
able to solve core development problems.