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

  • Home
  • SEARCH
  • 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 9166227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:06:27+00:00 2026-06-17T15:06:27+00:00

Say there is an extension method to order an IQueryable based on several types

  • 0

Say there is an extension method to order an IQueryable based on several types of Sorting (i.e. sorting by various properties) designated by a SortMethod enum.

public static IOrderedEnumerable<AClass> OrderByX(this IQueryable<AClass> values,
    SortMethod? sortMethod)
{ 
    IOrderedEnumerable<AClass> queryRes = null;
    switch (sortMethod)
    {
        case SortMethod.Method1:
            queryRes = values.OrderBy(a => a.Property1);
            break;
        case SortMethod.Method2:
            queryRes = values.OrderBy(a => a.Property2);
            break;
        case null:
            queryRes = values.OrderBy(a => a.DefaultProperty);
            break;
        default:
            queryRes = values.OrderBy(a => a.DefaultProperty);
            break;
    }
    return queryRes;
}

In the case where sortMethod is null (i.e. where it is specified that I don’t care about the order of the values), is there a way to instead of ordering by some default property, to instead just pass the IEnumerator values through as “ordered” without having to perform the actual sort?

I would like the ability to call this extension, and then possibly perform some additional ThenBy orderings.

  • 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-17T15:06:28+00:00Added an answer on June 17, 2026 at 3:06 pm

    All you need to do for the default case is:

    queryRes = values.OrderBy(a => 1);
    

    This will effectively be a noop sort. Because the OrderBy performs a stable sort the original order will be maintained in the event that the selected objects are equal. Note that since this is an IQueryable and not an IEnumerable it’s possible for the query provider to not perform a stable sort. In that case, you need to know if it’s important that order be maintained, or if it’s appropriate to just say “I don’t care what order the result is, so long as I can call ThenBy on the result).

    Another option, that allows you to avoid the actual sort is to create your own IOrderedEnumerable implementation:

    public class NoopOrder<T> : IOrderedEnumerable<T>
    {
        private IQueryable<T> source;
        public NoopOrder(IQueryable<T> source)
        {
            this.source = source;
        }
    
        public IOrderedEnumerable<T> CreateOrderedEnumerable<TKey>(Func<T, TKey> keySelector, IComparer<TKey> comparer, bool descending)
        {
            if (descending)
            {
                return source.OrderByDescending(keySelector, comparer);
            }
            else
            {
                return source.OrderBy(keySelector, comparer);
            }
        }
    
        public IEnumerator<T> GetEnumerator()
        {
            return source.GetEnumerator();
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return source.GetEnumerator();
        }
    }
    

    With that your query can be:

    queryRes = new NoopOrder<AClass>(values);
    

    Note that the consequence of the above class is that if there is a call to ThenBy that ThenBy will effectively be a top level sort. It is in effect turning the subsequent ThenBy into an OrderBy call. (This should not be surprising; ThenBy will call the CreateOrderedEnumerable method, and in there this code is calling OrderBy, basically turning that ThenBy into an OrderBy. From a conceptual sorting point of view, this is a way of saying that “all of the items in this sequence are equal in the eyes of this sort, but if you specify that equal objects should be tiebroken by something else, then do so.

    Another way of thinking of a “no op sort” is that it orders the items based in the index of the input sequence. This means that the items are not all “equal”, it means that the order input sequence will be the final order of the output sequence, and since each item in the input sequence is always larger than the one before it, adding additional “tiebreaker” comparisons will do nothing, making any subsequent ThenBy calls pointless. If this behavior is desired, it is even easier to implement than the previous one:

    public class NoopOrder<T> : IOrderedEnumerable<T>
    {
        private IQueryable<T> source;
        public NoopOrder(IQueryable<T> source)
        {
            this.source = source;
        }
    
        public IOrderedEnumerable<T> CreateOrderedEnumerable<TKey>(Func<T, TKey> keySelector, IComparer<TKey> comparer, bool descending)
        {
            return new NoopOrder<T>(source);
        }
    
        public IEnumerator<T> GetEnumerator()
        {
            return source.GetEnumerator();
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return source.GetEnumerator();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say there is a method Operation , public ObjectOut Operation(ObjectIn input) What is the
Is there some pedantic Enumerator-theory-based reason why the Any() method throws an ArgumentNullException for
Let's say I'm writing a jQuery extension method. This method should climb the ancestor
Say I have an overloaded extension method with the following two signatures: public static
Say there is an object A A has a strong reference to B B
Say there is: class A(B): ... where B could be object and ... is
Say there is a folder, '/home/user/temp/a40bd22344'. The name is completely random and changes in
Say there are multiple requests in a integration test, some of them are local
Say there are two objects, A and B , and there is a pointer
say there is an xml file, which not created by me, with a known

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.