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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:35:47+00:00 2026-06-14T02:35:47+00:00

I was reading a book about Linq, and saw that the Distinct method has

  • 0

I was reading a book about Linq, and saw that the Distinct method has an overload that takes a comparer. This would be a good solution to a problem I have where I want to get the distinct entities from a collection, but want the comparison to be on the entity ID, even if the other properties are different.

According to the book, if I have a Gribulator entity, I should be able to create a comparer like this…

private class GribulatorComparer : IComparer<Gribulator> {
  public int Compare(Gribulator g1, Gribulator g2) {
    return g1.ID.CompareTo(g2.ID);
  }
}

…and then use it like this…

List<Gribulator> distinctGribulators
  = myGribulators.Distinct(new GribulatorComparer()).ToList();

However, this gives the following compiler errors…

‘System.Collections.Generic.List’ does not contain a definition for ‘Distinct’ and the best extension method overload ‘System.Linq.Enumerable.Distinct(System.Collections.Generic.IEnumerable, System.Collections.Generic.IEqualityComparer)’ has some invalid arguments

Argument 2: cannot convert from ‘LinqPlayground.Program.GribulatorComparer’ to ‘System.Collections.Generic.IEqualityComparer’

I’ve searched around a bit, and have seen plenty of examples that use code like this, but no complaints about compiler errors.

What am I doing wrong? Also, is this the best way of doing this? I want a one-off solution here, so don’t want to start changing the code for the entity itself. I want the entity to remain as normal, but just in this one place, compare by ID only.

Thanks for any help.

  • 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-14T02:35:48+00:00Added an answer on June 14, 2026 at 2:35 am

    You’re implementing your comparer as an IComparer<T>, the LINQ method overload requires an implementation of IEqualityComparer:

    private class GribulatorComparer : IEqualityComparer<Gribulator> {
      public bool Equals(Gribulator g1, Gribulator g2) {
        return g1.ID == g2.ID;
      }
    }
    

    edit:

    For clarification, the IComparer interface can be used for sorting, as that’s basically what the Compare() method does.

    Like this:

    items.OrderBy(x => new ItemComparer());
    
    private class ItemComparer : IComparer<Item>
    {
        public int Compare(Item x, Item y)
        {
            return x.Id.CompareTo(y.Id)
        }
    }
    

    Which will sort your collection using that comparer, however LINQ provides a way to do that for simple fields (like an int Id).

    items.OrderBy(x => x.Id);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reading a book about SQL. In that book, I saw strange query below:
I was reading a book about Javascript and saw this line; JavaScript does not
After reading a book on LINQ I'm thinking about re-writing a mapper class that
Recently, I was reading book about Erlang which has hot deployment feature. The deployment
I'm reading a book about SQL. In that book there's the term Ad Hoc
I'm reading a book about DI that always talks about DI frameworks instantiating an
I'm reading about STL algorithms and the book pointed out that algorithms like find
I am reading about closures in Javascript: The Good Parts book. There is following
Reading a book about C# I noticed that sometimes is mentioned value type and
I was reading a book about AJAX and the writer said that one should

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.