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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:16:17+00:00 2026-05-20T17:16:17+00:00

I want to write a new override of Except extension method for IEnumerable which

  • 0

I want to write a new override of Except extension method for IEnumerable which is able to take a comparer inline instead of using IEqualityComparer.

A, B are collections of a reference type..

Something like this:

A: [1, A], [2, A], [3, A]

B: [3, B], [4, B], [5, B]

C = A.Except(B, (a,b) => a.Id == b.Id);

C: [1, A], [2, A]

I wonder if you could help me with the code of the method.

public static class IEnumerableExntesion
    {
        public IEnumerable<T> Except<T>(this IEnumerable<T> source, 
                                        IEnumerable<T> second, 
                                        Func<T, T, bool> predicate)
        {

        }
    }

I was thinking of:

return source.Where (s => !second.Any(p => p.Id == s.Id));

But actually I couldn’t convert it to a generic solution using the passed predicate!

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-05-20T17:16:18+00:00Added an answer on May 20, 2026 at 5:16 pm

    Do you need to do the comparison using a predicate or can you use a projection instead and compare the projected values? If so then you could use some sort of ExceptBy method:

    var c = a.ExceptBy(b, x => x.Id);
    
    var r = p.ExceptBy(q, x => x.Name, StringComparer.OrdinalIgnoreCase);
    
    // ...
    
    public static class EnumerableExtensions
    {
        public static IEnumerable<TSource> ExceptBy<TSource, TKey>(
            this IEnumerable<TSource> first, IEnumerable<TSource> second,
            Func<TSource, TKey> keySelector,
            IEqualityComparer<TKey> keyComparer = null)
        {
            if (first == null) throw new ArgumentNullException("first");
            if (second == null) throw new ArgumentNullException("second");
            if (keySelector == null) throw new ArgumentNullException("keySelector");
    
            return first.ExceptByIterator(second, keySelector, keyComparer);
        }
    
        private static IEnumerable<TSource> ExceptByIterator<TSource, TKey>(
            this IEnumerable<TSource> first, IEnumerable<TSource> second,
            Func<TSource, TKey> keySelector, IEqualityComparer<TKey> keyComparer)
        {
            var keys = new HashSet<TKey>(second.Select(keySelector), keyComparer);
    
            foreach (TSource item in first)
            {
                if (keys.Add(keySelector(item)))
                    yield return item;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Callback-Method setStatusCallback() . I want to override the Action-Method statusAction() which
I want to write something like: var list = new List<int>(){1,2,3}; var bigList =
I am new on Django and Python. I want to write a program with
I want write a little code analyzer which parses nested structures and translates into
I want to write a syntax highlighting extension for Emacs, but I Googling of
I want to write a simple web framework myself using WSGI, Python. I am
I want to make registration form and write the data into data.txt using ActiveXObject.
I want to develop logging system in OSGI bundle which can write application errors
I want to implement OSGI bundle which can write error messages into log file.
I want to write a DBI wrapper, provide select/insert/update/delete, and users can choose which

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.