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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:27:15+00:00 2026-05-10T19:27:15+00:00

So .NET 3.0/3.5 provides us with lots of new ways to query, sort, and

  • 0

So .NET 3.0/3.5 provides us with lots of new ways to query, sort, and manipulate data, thanks to all the neat functions supplied with LINQ. Sometimes, I need to compare user-defined types that don’t have a built-in comparison operator. In many cases, the comparison is really simple — something like foo1.key ?= foo2.key. Rather than creating a new IEqualityComparer for the type, can I simply specify the comparison inline using anonymous delegates/lambda functions? Something like:

var f1 = ...,     f2 = ...; var f3 = f1.Except(            f2, new IEqualityComparer(              (Foo a, Foo b) => a.key.CompareTo(b.key)            ) ); 

I’m pretty sure the above doesn’t actually work. I just don’t want to have to make something as ‘heavy’ as a whole class just to tell the program how to compare apples to apples.

  • 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-10T19:27:16+00:00Added an answer on May 10, 2026 at 7:27 pm

    My MiscUtil library contains a ProjectionComparer to build an IComparer<T> from a projection delegate. It would be the work of 10 minutes to make a ProjectionEqualityComparer to do the same thing.

    EDIT: Here’s the code for ProjectionEqualityComparer:

    using System; using System.Collections.Generic;  /// <summary> /// Non-generic class to produce instances of the generic class, /// optionally using type inference. /// </summary> public static class ProjectionEqualityComparer {     /// <summary>     /// Creates an instance of ProjectionEqualityComparer using the specified projection.     /// </summary>     /// <typeparam name='TSource'>Type parameter for the elements to be compared</typeparam>     /// <typeparam name='TKey'>Type parameter for the keys to be compared,     /// after being projected from the elements</typeparam>     /// <param name='projection'>Projection to use when determining the key of an element</param>     /// <returns>A comparer which will compare elements by projecting      /// each element to its key, and comparing keys</returns>     public static ProjectionEqualityComparer<TSource, TKey> Create<TSource, TKey>(Func<TSource, TKey> projection)     {         return new ProjectionEqualityComparer<TSource, TKey>(projection);     }      /// <summary>     /// Creates an instance of ProjectionEqualityComparer using the specified projection.     /// The ignored parameter is solely present to aid type inference.     /// </summary>     /// <typeparam name='TSource'>Type parameter for the elements to be compared</typeparam>     /// <typeparam name='TKey'>Type parameter for the keys to be compared,     /// after being projected from the elements</typeparam>     /// <param name='ignored'>Value is ignored - type may be used by type inference</param>     /// <param name='projection'>Projection to use when determining the key of an element</param>     /// <returns>A comparer which will compare elements by projecting     /// each element to its key, and comparing keys</returns>     public static ProjectionEqualityComparer<TSource, TKey> Create<TSource, TKey>         (TSource ignored,          Func<TSource, TKey> projection)     {         return new ProjectionEqualityComparer<TSource, TKey>(projection);     }  }  /// <summary> /// Class generic in the source only to produce instances of the  /// doubly generic class, optionally using type inference. /// </summary> public static class ProjectionEqualityComparer<TSource> {     /// <summary>     /// Creates an instance of ProjectionEqualityComparer using the specified projection.     /// </summary>     /// <typeparam name='TKey'>Type parameter for the keys to be compared,     /// after being projected from the elements</typeparam>     /// <param name='projection'>Projection to use when determining the key of an element</param>     /// <returns>A comparer which will compare elements by projecting each element to its key,     /// and comparing keys</returns>             public static ProjectionEqualityComparer<TSource, TKey> Create<TKey>(Func<TSource, TKey> projection)     {         return new ProjectionEqualityComparer<TSource, TKey>(projection);     } }  /// <summary> /// Comparer which projects each element of the comparison to a key, and then compares /// those keys using the specified (or default) comparer for the key type. /// </summary> /// <typeparam name='TSource'>Type of elements which this comparer  /// will be asked to compare</typeparam> /// <typeparam name='TKey'>Type of the key projected /// from the element</typeparam> public class ProjectionEqualityComparer<TSource, TKey> : IEqualityComparer<TSource> {     readonly Func<TSource, TKey> projection;     readonly IEqualityComparer<TKey> comparer;      /// <summary>     /// Creates a new instance using the specified projection, which must not be null.     /// The default comparer for the projected type is used.     /// </summary>     /// <param name='projection'>Projection to use during comparisons</param>     public ProjectionEqualityComparer(Func<TSource, TKey> projection)         : this(projection, null)     {     }      /// <summary>     /// Creates a new instance using the specified projection, which must not be null.     /// </summary>     /// <param name='projection'>Projection to use during comparisons</param>     /// <param name='comparer'>The comparer to use on the keys. May be null, in     /// which case the default comparer will be used.</param>     public ProjectionEqualityComparer(Func<TSource, TKey> projection, IEqualityComparer<TKey> comparer)     {         if (projection == null)         {             throw new ArgumentNullException('projection');         }         this.comparer = comparer ?? EqualityComparer<TKey>.Default;         this.projection = projection;     }      /// <summary>     /// Compares the two specified values for equality by applying the projection     /// to each value and then using the equality comparer on the resulting keys. Null     /// references are never passed to the projection.     /// </summary>     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));     }      /// <summary>     /// Produces a hash code for the given value by projecting it and     /// then asking the equality comparer to find the hash code of     /// the resulting key.     /// </summary>     public int GetHashCode(TSource obj)     {         if (obj == null)         {             throw new ArgumentNullException('obj');         }         return comparer.GetHashCode(projection(obj));     } } 

    And here’s a sample use:

    var f3 = f1.Except(f2, ProjectionEqualityComparer<Foo>.Create(a => a.key)); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 66k
  • Answers 66k
  • 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 You can create multiple app domains in a single program/process.… May 11, 2026 at 11:31 am
  • added an answer temptable.Rows[e.RowIndex].Delete() Does it solve your problem? May 11, 2026 at 11:31 am
  • added an answer That's the deal when working with legacy code. Legacy meaning… May 11, 2026 at 11:31 am

Related Questions

So .NET 3.0/3.5 provides us with lots of new ways to query, sort, and
I have just started to look at .NET 3.5 so please forgive me if
Does SQL Server 2008 ship with the .NET 3.5 CLR, so that stored procedures
I've installed .NET Framework 3.5 SP1 on web server (Server 2008 Enterprise), so running
So the .NET framework provides the SecureString class for storing strings in a secure
I am new to visual studio/asp.net so please bear with me. Using vs 2005
I'm new to jquery and asp.net so please forgive if this is an obvious
Am I able to embed the .net runtime so that .net is not required
I want to extend the basic ControlCollection in VB.NET so I can just add
I've written 2 reasonably large scale apps in .net so far, and both of

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.