Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8616111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:32:26+00:00 2026-06-12T05:32:26+00:00

I am rewriting an application which needs to duplicate the alphabetization logic of an

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:32:27+00:00Added an answer on June 12, 2026 at 5:32 am

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a build system for my web application that's rewriting all resource
We're currently rewriting our organizations ASP.NET MVC application which has been written twice already.
I'm rewriting an application which handles a lot of data (about 100 GB) which
I have been tasked with rewriting a .NET application which communicates using a WSE
I am rewriting a big piece of our application which requires a user to
I'm rewriting an LOB application whose architecture is like this: Silverlight && Windows Mobile
I'm rewriting a data-driven legacy application in Python. One of the primary tables is
I am rewriting the existing C++ application and adapting it for Android environment. In
I'm rewriting a multithread Linux-2.6.32+ application to replace select with epoll . The man
I'm trying to do link rewriting in my mobile application (written in ruby). I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.