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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:08:38+00:00 2026-05-11T14:08:38+00:00

Short question: Is there a simple way in LINQ to objects to get a

  • 0

Short question:

Is there a simple way in LINQ to objects to get a distinct list of objects from a list based on a key property on the objects.

Long question:

I am trying to do a Distinct() operation on a list of objects that have a key as one of their properties.

class GalleryImage {    public int Key { get;set; }    public string Caption { get;set; }    public string Filename { get; set; }    public string[] Tags {g et; set; } } 

I have a list of Gallery objects that contain GalleryImage[].

Because of the way the webservice works [sic] I have duplicates of the GalleryImage object. i thought it would be a simple matter to use Distinct() to get a distinct list.

This is the LINQ query I want to use :

var allImages = Galleries.SelectMany(x => x.Images); var distinctImages = allImages.Distinct<GalleryImage>(new                       EqualityComparer<GalleryImage>((a, b) => a.id == b.id)); 

The problem is that EqualityComparer is an abstract class.

I dont want to :

  • implement IEquatable on GalleryImage because it is generated
  • have to write a separate class to implement IEqualityComparer as shown here

Is there a concrete implementation of EqualityComparer somewhere that I’m missing?

I would have thought there would be an easy way to get ‘distinct’ objects from a set based on a key.

  • 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. 2026-05-11T14:08:39+00:00Added an answer on May 11, 2026 at 2:08 pm

    (There are two solutions here – see the end for the second one):

    My MiscUtil library has a ProjectionEqualityComparer class (and two supporting classes to make use of type inference).

    Here’s an example of using it:

    EqualityComparer<GalleryImage> comparer =      ProjectionEqualityComparer<GalleryImage>.Create(x => x.id); 

    Here’s the code (comments removed)

    // Helper class for construction public static class ProjectionEqualityComparer {     public static ProjectionEqualityComparer<TSource, TKey>         Create<TSource, TKey>(Func<TSource, TKey> projection)     {         return new ProjectionEqualityComparer<TSource, TKey>(projection);     }      public static ProjectionEqualityComparer<TSource, TKey>         Create<TSource, TKey> (TSource ignored,                                Func<TSource, TKey> projection)     {         return new ProjectionEqualityComparer<TSource, TKey>(projection);     } }  public static class ProjectionEqualityComparer<TSource> {     public static ProjectionEqualityComparer<TSource, TKey>         Create<TKey>(Func<TSource, TKey> projection)     {         return new ProjectionEqualityComparer<TSource, TKey>(projection);     } }  public class ProjectionEqualityComparer<TSource, TKey>     : IEqualityComparer<TSource> {     readonly Func<TSource, TKey> projection;     readonly IEqualityComparer<TKey> comparer;      public ProjectionEqualityComparer(Func<TSource, TKey> projection)         : this(projection, null)     {     }      public ProjectionEqualityComparer(         Func<TSource, TKey> projection,         IEqualityComparer<TKey> comparer)     {         projection.ThrowIfNull('projection');         this.comparer = comparer ?? EqualityComparer<TKey>.Default;         this.projection = projection;     }      public bool Equals(TSource x, TSource y)     {         if (x == null && y == null)         {             return true;         }         if (x == null || y == null)         {             return false;         }         return comparer.Equals(projection(x), projection(y));     }      public int GetHashCode(TSource obj)     {         if (obj == null)         {             throw new ArgumentNullException('obj');         }         return comparer.GetHashCode(projection(obj));     } } 

    Second solution

    To do this just for Distinct, you can use the DistinctBy extension in MoreLINQ:

        public static IEnumerable<TSource> DistinctBy<TSource, TKey>         (this IEnumerable<TSource> source,          Func<TSource, TKey> keySelector)     {         return source.DistinctBy(keySelector, null);     }      public static IEnumerable<TSource> DistinctBy<TSource, TKey>         (this IEnumerable<TSource> source,          Func<TSource, TKey> keySelector,          IEqualityComparer<TKey> comparer)     {         source.ThrowIfNull('source');         keySelector.ThrowIfNull('keySelector');         return DistinctByImpl(source, keySelector, comparer);     }      private static IEnumerable<TSource> DistinctByImpl<TSource, TKey>         (IEnumerable<TSource> source,          Func<TSource, TKey> keySelector,          IEqualityComparer<TKey> comparer)     {         HashSet<TKey> knownKeys = new HashSet<TKey>(comparer);         foreach (TSource element in source)         {             if (knownKeys.Add(keySelector(element)))             {                 yield return element;             }         }     } 

    In both cases, ThrowIfNull looks like this:

    public static void ThrowIfNull<T>(this T data, string name) where T : class {     if (data == null)     {         throw new ArgumentNullException(name);     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 76k
  • Answers 76k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I'd say it depends. Some things you will find using… May 11, 2026 at 3:14 pm
  • added an answer The color brightwhite looks ok on my Emacs (which is… May 11, 2026 at 3:14 pm
  • added an answer You create an inner class because it is only ever… May 11, 2026 at 3:14 pm

Related Questions

I'm trying to decide on the best strategy for accessing the database. I understand
I'm trying to build some code for dynamically sorting a Linq IQueryable<>. The obvious
I have successfully set up a WPF Datagrid with the March 2009 WPF Toolkit
I have two directories in the same parent directory. Call the parent directory base

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.