I have encountered some odd behaviour while implementing a Group Join with a customer IEqualityComparer.
The following code demonstrates the behaviour that is the problem for me
List<String> inner = new List<string>() { "i1", "i2" };
List<String> outer = new List<string>() { "o1", "o2" };
var grouped = outer.GroupJoin(inner, i => i, o=> o, (inKey, outCollection) => new {Key = inKey, List = outCollection},
new EqualityComparer<string>((i, o) => i == o)).ToList();
From the docs found on MSDN I would expect that the last parameter to be passed a series of inner keys and outer keys for comparison.
However, placing a breakpoint inside the Func shows that both i and o start with the letter i and are in fact both elements of the inner collection so the grouped
object is always empty (I know the example will always be empty, its just the smallest bit of code that that demonstrates the problem).
Is there a way to GroupJoin objects with a custom comparator?
For completeness, this the EqualityComparer that is being created in the GroupJoin argument list:
public class EqualityComparer<T> : IEqualityComparer<T>
{
public EqualityComparer(Func<T, T, bool> cmp)
{
this.cmp = cmp;
}
public bool Equals(T x, T y)
{
return cmp(x, y);
}
public int GetHashCode(T obj)
{
// Always return 0 so that the function is called
return 0;
}
public Func<T, T, bool> cmp { get; set; }
}