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 7857847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:10:24+00:00 2026-06-02T21:10:24+00:00

Ive been trying to get OrderBy in a LINQ statement to work with an

  • 0

Ive been trying to get OrderBy in a LINQ statement to work with an anonymous object but have failed by now.

I checked these already:
Anonymous IComparer implementation
C# linq sort – quick way of instantiating IComparer
How to sort an array of object by a specific field in C#?

I spent a few hours trying different approaches but there has to be something I’m missing.

Let’s say there’s the following class:

public class Product
{
   public int Id {get; set;}
   public string Name {get; set;}
   public int Popularity {get; set;}
   public decimal Price {get; set;}
}

And products is a list of these objects.

How can I complete this LINQ statement, so that it works with the anonymous object ?
To be clear, I know I can do this in a different way but I’d be very interested to learn how to make this particular example work.

var sortedProducts = products
                       .OrderBy(p => 
                              new {p.Popularity, p.Price}, 
                              [IComparer magic goes here]);

It seems that it should be possible with an implementation of the ProjectionComparer:
http://code.google.com/p/edulinq/source/browse/src/Edulinq/ProjectionComparer.cs?r=0c583631b709679831c99df2646fc9adb781b2be

Any ideas how to do this ?

UPDATE:

I did a quick performance test on this – the anonymous comparer solution vs standard orderby.thenby and it seems that the anonymous solution is quite slower which is probably what we might have expected anyway.

         numProd  | Anon    | chained orderby clauses
         10 000   | 47 ms   | 31 ms
         100 000  | 468 ms  | 234 ms
         1 000 000| 5818 ms | 2387 ms
         5 000 000| 29547 ms| 12105 ms
  • 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-02T21:10:26+00:00Added an answer on June 2, 2026 at 9:10 pm

    You can make an IComparer<T> implementation that uses a delegate that you supply for the comparison, and instantiate it with type inference (similar to “cast by example”):

    static class AnonymousComparer
    {
        public static IComparer<T> GetComparer<T>(T example, Comparison<T> comparison)
        {
            return new ComparerImpl<T>(comparison);
        }
        private class ComparerImpl<T> : IComparer<T>
        {
            private readonly Comparison<T> _comparison;
            public ComparerImpl(Comparison<T> comparison) { _comparison = comparison; }
            public int Compare(T x, T y) { return _comparison.Invoke(x, y); }
        }
    }
    

    And use it thus:

    var comparer = AnonymousComparer.GetComparer(
        new { Popularity = 0, Price = 0m },
        (a, b) => //comparison logic goes here
        );
    
    var sortedProducts = products
        .OrderBy(p =>
            new { p.Popularity, p.Price },
            comparer); 
    

    EDIT: I just checked out the projection comparer page you linked to. With that approach, you don’t need the “example” argument for type inference. The approach still needs to be adapted, however, to take a delegate instead of an interface. Here it is:

    //adapted from http://code.google.com/p/edulinq/source/browse/src/Edulinq/ProjectionComparer.cs?r=0c583631b709679831c99df2646fc9adb781b2be
    static class AnonymousProjectionComparer
    {
        private class ProjectionComparer<TElement, TKey> : IComparer<TElement>
        {
            private readonly Func<TElement, TKey> keySelector;
            private readonly Comparison<TKey> comparison;
    
            internal ProjectionComparer(Func<TElement, TKey> keySelector, Comparison<TKey> comparison)
            {
                this.keySelector = keySelector;
                this.comparison = comparison ?? Comparer<TKey>.Default.Compare;
            }
    
            public int Compare(TElement x, TElement y)
            {
                TKey keyX = keySelector(x);
                TKey keyY = keySelector(y);
                return comparison.Invoke(keyX, keyY);
            }
        }
    
        public static IComparer<TElement> GetComparer<TElement, TKey>(Func<TElement, TKey> keySelector, Comparison<TKey> comparison)
        {
            return new ProjectionComparer<TElement, TKey>(keySelector, comparison);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ive been trying to get this sql statement to work properly through excel vba.
Ive been trying to get this to work for 3 days now. I cant
Ive been trying to get this solved for quite a few hours, but I
Ive been trying to debug this for hours but I was unable to get
Ive been trying to get this to work for the last week and cannot
I've been trying to get the UNIQUE constraint placed on some attributes I have
I've been trying to get this module to work and no matter what I've
I've been trying to get colourschemes to work properly in VIM when using it
I've been trying to get setup with Ruby on Rails today, but I think
I've been trying to get an Gtk2::Image object in this Perl Gtk2 application to

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.