I am rewriting an application which needs to duplicate the alphabetization logic of an old mainframe system. In the old mainframe system, item change record IDs are alphabetized From A through Z, and then starts over with AA, AB, and so on. Unfortunately both SQL Server and .NET want to put AA between A and B, so I’m having to jump through some hoops. I’m trying to sort the change IDs first by descending length and then by alphabetizing in descending order.
Here is the method I am using to retrieve the data:
protected internal IList<TeamViewModel> GetTeams(string recordId, string changeId)
{
var viewModels = (from x in repository.Teams
where x.RecordId == recordId && x.ChangeId.Length <= changeId.Length &&
(x.ChangeId.CompareTo(changeId) == -1 || x.ChangeId.CompareTo(changeId) == 0)
orderby x.ChangeId.Length descending, x.ChangeId descending, x.ChangeId descending
group x by x.TeamId into grp
select grp.FirstOrDefault())
.ToList()
.Select(TeamViewModel.FromEntity)
.ToList();
return viewModels;
}
Each “Record” has a collection of change records, and each change record a ChangeId property and and TeamId that made the change. I’m trying to get the “newest” (according to the old mainframe sorting logic) change record for each distinct teamId. In other words, I am attempting to order the records, group them by team, and then grab the first record from each group.
The repository.Teams property returns an IQueryable which wraps the ObjectSet declared in my ObjectContext.
What really blows my mind is that this query works fine when I run it in Linqpad (with default configuration) and this very method executes fine from within unit tests when I have injected a mock of my repository into this class, which I have setup to emit the exact same data that is in the SQL database.
But when this method is executed at runtime, it behaves as though the line with the orderbys is completely omitted and in fact gives me the same results that I ge tin Linqpad (with default configuration) when I comment out the line with the orderbys. SQL Profiler shows that the generated SQL is nigh impossible to decipher by a human, but which does not contain the word “order” in it anywhere.
As a final note, when I configure Linqpad to use my EF-generated typed dataset within my project’s assembly, I get the same results that I see at run time, with results appearing as though the orderbys are disregarded.
I wish I could show actual results but the data is proprietary, so just consider that the correct results, as provided by Linqpad and my unit tests, contain change records with IDs like “Y” and “Z” where as the inexplicable (to me) results that I see in my project at runtime are like “A” and “B”.
Can anyone see what is occurring here, and just as importantly, what I need to change to make this function as I am expecting?
My project is using entity framework 5.0 and I’m using Linqpad 4.42.01.
Thanks so much!
Group by does not guarantee to retain the original order. If you want to order, you must order after the group by.
In fact, it doesn’t even make sense to order by before a group by, since the group by is guaranteed to change the order (unless there’s only a single group).