Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is LINQ Join operator using Nested Loop, Merge, or HashSet Joins?

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

like image 636
aattia Avatar asked Oct 13 '09 17:10

aattia


1 Answers

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.

like image 170
Jon Skeet Avatar answered Sep 19 '22 19:09

Jon Skeet