Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Except and Intersect overloads that take delegates #80

Open
GoogleCodeExporter opened this issue Dec 3, 2015 · 1 comment
Open

Except and Intersect overloads that take delegates #80

GoogleCodeExporter opened this issue Dec 3, 2015 · 1 comment

Comments

@GoogleCodeExporter
Copy link

Useful when you have one sequence, and you want to get the except/intersect 
from another sequence based on a specific projection (like DistinctBy)

Originally sourced from here: 
http://akshayluther.com/2009/08/14/improving-linq-except-and-intersect/

Additional overloads added where the two sequences have different types added - 
since the Func doesn't really care if they're the same or not and therefore you 
can just check equality inside.

Sample usage:
var toRemove = existingResults.Except(newResults, (x, y) => x.EntityId == 
y.EntityId && x.EntityType == y.EntityType);

Code: 
 public static IEnumerable<TSource> Except<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second, Func<TSource, TSource, bool> comparer)
        {
            return first.Where(x => second.Count(y => comparer(x, y)) == 0);
        }

        public static IEnumerable<TSource> Intersect<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second, Func<TSource, TSource, bool> comparer)
        {
            return first.Where(x => second.Count(y => comparer(x, y)) == 1);
        }

        public static IEnumerable<TFirstSource> Except<TFirstSource, TSecondSource>(this IEnumerable<TFirstSource> first, IEnumerable<TSecondSource> second, Func<TFirstSource, TSecondSource, bool> comparer)
        {
            return first.Where(x => second.Count(y => comparer(x, y)) == 0);
        }

        public static IEnumerable<TFirstSource> Intersect<TFirstSource, TSecondSource>(this IEnumerable<TFirstSource> first, IEnumerable<TSecondSource> second, Func<TFirstSource, TSecondSource, bool> comparer)
        {
            return first.Where(x => second.Count(y => comparer(x, y)) == 1);
        }

Original issue reported on code.google.com by agr...@gmail.com on 9 Jul 2013 at 10:34

@GoogleCodeExporter
Copy link
Author

This issue has been migrated to:
https://github.com/MoreLINQ/morelinq/issues/80
The conversation continues there.
DO NOT post any further comments to the issue tracker on Google Code as it is 
shutting down.
You can also just subscribe to the issue on GitHub to receive notifications of 
any further development.

Original comment by azizatif on 21 Aug 2015 at 6:55

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant