I came across the following in some code that I was refactoring today.
context.Entities.Where(x => x.ForeignKeyId == id)
.OrderBy(x => x.FirstSortField)
.OrderBy(x => x.SecondSortField);
Initially, I took out the .OrderBy(x => x.FirstSortField)
thinking that the first OrderBy statement would just be replaced by the second OrderBy. After testing, I realized that it was generating the SQL as ORDER BY SecondSortField, FirstSortField
.
Therefore, the equivalent is actually:
context.Entities.Where(x => x.ForeignKeyId == id)
.OrderBy(x => x.SecondSortField)
.ThenBy(x => x.FirstSortField);
Can anyone explain the reasoning behind EF6 doing this? It seems to me that it would be more intuitive to replace the first sort field with the second.