Does anyone know what Join algorith does LINQ performs with its Join operator.
Is it NestedLoop, Merge, or HashSet? Is there any way to specify a different one, if supported?
Regards Albert
First it effectively creates a lookup from the "inner" sequence, then iterates through the outer sequence. It can then look up each key from the outer sequence and yield each appropriate pair. Something like this (ignoring argument validation etc):
public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>
(this IEnumerable<TOuter> outer,
IEnumerable<TInner> inner,
Func<TOuter, TKey> outerKeySelector,
Func<TInner, TKey> innerKeySelector,
Func<TOuter, TInner, TResult> resultSelector)
{
Lookup<TKey, TInner> lookup = inner.ToLookup(innerKeySelector);
foreach (TOuter outerItem in outer)
{
TKey key = outerKeySelector(outerItem);
foreach (TInner innerItem in lookup[key])
{
yield return resultSelector(outerItem, innerItem);
}
}
}
The lookup will use a hash table internally for the keys, so that it's efficient to look up any individual key.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With