Duplicate Edit: Not a duplicate since all the information that was delivered in the responses is already contained in the question. I was wondering if anyone knew a workaround or at least the expected behavior and that question has not been answered...
(Follow Up question to my last question)
I'm trying to use .GroupJoin() to map a list of keys to another list of keys, except it's also a match if the keys are equal after ignoring capitalization or certain escape-sequences.
For example:
I have a key called "ABCD"
in my first list. A list of "Equals" could consist of: {"ABCD", "AbCd", "AB_MINUS_CD", "ab_PLUS_cd"}
.
My custom "Equals" checks against a pre-defined list of Escape sequences:
Equals:
public bool Equals(string firstKey, string escapedKey)
{
int counter = 0;
while (escapedKey.Count(x => x == '_') >= 2)
{
CharacterMaping mapping = Mappings.ElementAt(counter);
escapedKey = escapedKey.Replace(mapping, "");
}
return String.Equals(firstKey, escapedKey, StringComparison.CurrentCultureIgnoreCase);
}
GroupJoin:
IEnumerable<KeyMapping> keyMappings = cleanKeys.GroupJoin(
escapedKeys,
c => c,
e => e,
(c, result) => new KeyMapping(c, result),
_keyComparer);
Hoever, I see two problems with this:
- To Implement
IEqualityComparer<string>
I also need to implementGetHashCode()
. My assumption right now is that hashing is considered faster than Equals so it first checks for Equality of HashCodes and only if the HashCodes are equal ( = Equality is possible ) it also checks for Equality usingEquals();
Since a successful Hash doesn't also automatically mean equality (collisions are possible after all) I might be able to hack around it for my case by simple makingGetHashCode()
return the same value, regardless of the input. - By definition an
IEqualityComparer
(or rather any type of equivalence relation needs to be symmetric:a == b => b == a
. I am usingIEqualityComparer
becauseGroupJoin
requires it but I am obviously trying to write a one-directionalIEqualityComparer
:a == b
does not implyb == a
. I am, again, just assuming thatGroupJoin
callsEquals(a, b)
with the element of the first list as the first argument but it would be good to know if I have to reverse it :p
Honestly, I am trying to make something work that is just not meant to be that way and I should probably use a different, more manual, loop-based approach but I am really intrigued and wondering if someone can tell me, how this stuff works internally.