-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically remove duplicate recipients.
Resolves #229
- Loading branch information
Showing
3 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace StrongGrid.Utilities | ||
{ | ||
/// <summary> | ||
/// LambdaComparer - avoids the need for writing custom IEqualityComparers | ||
/// Usage: | ||
/// List<MyObject> x = myCollection.Except(otherCollection, new LambdaComparer<MyObject>((x, y) => x.Id == y.Id)).ToList(); | ||
/// or | ||
/// IEqualityComparer comparer = new LambdaComparer<MyObject>((x, y) => x.Id == y.Id); | ||
/// List<MyObject> x = myCollection.Except(otherCollection, comparer).ToList(); | ||
/// </summary> | ||
/// <typeparam name="T">The type to compare</typeparam> | ||
/// <remarks>From: http://toreaurstad.blogspot.ca/2014/06/a-generic-iequalitycomparer-of-t.html</remarks> | ||
internal class LambdaComparer<T> : IEqualityComparer<T> | ||
{ | ||
private readonly Func<T, T, bool> _lambdaComparer; | ||
private readonly Func<T, int> _lambdaHash; | ||
|
||
public LambdaComparer(Func<T, T, bool> lambdaComparer) | ||
: this(lambdaComparer, o => 0) | ||
{ | ||
} | ||
|
||
public LambdaComparer(Func<T, T, bool> lambdaComparer, Func<T, int> lambdaHash) | ||
{ | ||
_lambdaComparer = lambdaComparer ?? throw new ArgumentNullException(nameof(lambdaComparer)); | ||
_lambdaHash = lambdaHash ?? throw new ArgumentNullException(nameof(lambdaHash)); | ||
} | ||
|
||
public bool Equals(T x, T y) | ||
{ | ||
return _lambdaComparer(x, y); | ||
} | ||
|
||
public int GetHashCode(T obj) | ||
{ | ||
return _lambdaHash(obj); | ||
} | ||
} | ||
} |