This question is locked. New answers and comments are not allowed.
In the following example, the executed query is not doing a distinct at the database level event though the grade name is a string property. It appears to bring back all records and then do the distinct in memory. Possibly related to the multiple column sort?
It results in the following query:
SELECT
c.[Name] AS COL1, c.[SortOrder] AS xj1
FROM
[Sf_SchoolProduct] a JOIN [Sf_Gender] AS b ON (a.[GenderCode] = b.[Code]) LEFT JOIN [Sf_Grade] AS c ON (a.[GradeCode] = c.[Code])
WHERE
a.[SchoolCode] = @p0 AND b.[Name] = @p1 ORDER BY xj1, COL1
//School and genderName are method arguments.
using (MyContext context = new MyContext ())
{
return context.SchoolProducts
.Where(sp => sp.SchoolCode == School.Code && sp.Gender.Name == genderName)
.OrderBy(sp => sp.Grade.SortOrder)
.ThenBy(sp => sp.Grade.Name)
.Select(sp => sp.Grade.Name)
.Distinct()
.ToList();
}