I have attempted to create an extension method that looks like this…
public static IEnumerable<T> Distinct<T>(this IEnumerable<T> value, IEnumerable<T> compareTo, Func<T, object> compareFieldPredicate)
{
return value.Where(o => !compareTo.Exists(p => compareFieldPredicate.Invoke(p) == compareFieldPredicate.Invoke(o)));
}
The idea is that I would be able to do something like this…
IEnumerable<MyCollection> distinctValues = MyCollection.Distinct(MyOtherCollection, o => o.ID); //Note that o.ID is a guid
Now at this point I would have expected to have only my distinct items returned to me but what I found is that this was never the case.
Upon further research breaking down this method using the following code.
Guid guid1 = Guid.NewGuid();
Guid guid2 = new Guid(guid1.ToString());
Func<MyObject, object> myFunction = o => o.ID;
Func<MyObject, object> myFunction1 = o => o.ID;
bool result = myFunction(MyObject) == myFunction1(MyObject);
//result = false
I have found that infact even if the Guids are the same the comparison will always return false.
What is the cause of this?
Your problem is that you’re boxing the Guids into Objects before you compare them. Consider this code:
That actually outputs:
Since “o1” and “o2”, while equal to the same Guid, are not the same object.
If you truly want your “Distinct” extension method to not be tied to a specific type (like Guid), you can do this: