5

The LINQ Join() method with Nullable<int> for TKey skips over null key matches. What am I missing in the documentation? I know that I can switch to SelectMany(), I'm just curious why this equality operation works like SQL and not like C# since as near as I can tell, the EqualityComparer<int?>.Default works exactly like I would expect it to for null values.

http://msdn.microsoft.com/en-us/library/bb534675.aspx

using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;

public class dt
{
   public int? Id;
   public string Data;
}

public class JoinTest
{
    public static int Main(string [] args)
    {
        var a = new List<dt>
        {
            new dt { Id = null, Data = "null" },
            new dt { Id = 1, Data = "1" },
            new dt { Id = 2, Data = "2" }
        };

        var b = new List<dt>
        {
            new dt { Id = null, Data = "NULL" },
            new dt { Id = 2, Data = "two" },
            new dt { Id = 3, Data = "three" }
        };

        //Join with null elements
        var c = a.Join( b,
            dtA => dtA.Id,
            dtB => dtB.Id,
            (dtA, dtB) => new { aData = dtA.Data, bData = dtB.Data } ).ToList();
        // Output:
        // 2 two
        foreach ( var aC in c )
            Console.WriteLine( aC.aData + " " + aC.bData );
        Console.WriteLine( " " );

        //Join with null elements converted to zero
        c = a.Join( b,
            dtA => dtA.Id.GetValueOrDefault(),
            dtB => dtB.Id.GetValueOrDefault(),
            (dtA, dtB) => new { aData = dtA.Data, bData = dtB.Data } ).ToList();

        // Output:
        // null NULL
        // 2 two
        foreach ( var aC in c )
            Console.WriteLine( aC.aData + " " + aC.bData );

        Console.WriteLine( EqualityComparer<int?>.Default.Equals( a[0].Id, b[0].Id ) );
        Console.WriteLine( EqualityComparer<object>.Default.Equals( a[0].Id, b[0].Id ) );
        Console.WriteLine( a[0].Id.Equals( b[0].Id ) );

        return 0;
    }
}

2 Answers 2

5

Enumerable.Join uses JoinIterator (private class) to iterate over matching elements. JoinIterator uses Lookup<TKey, TElement> for creating lookups of sequence keys:

internal static Lookup<TKey, TElement> CreateForJoin(
    IEnumerable<TElement> source, 
    Func<TElement, TKey> keySelector, 
    IEqualityComparer<TKey> comparer)
{
    Lookup<TKey, TElement> lookup = new Lookup<TKey, TElement>(comparer);
    foreach (TElement local in source)
    {
        TKey key = keySelector(local);
        if (key != null) // <--- Here
        {
            lookup.GetGrouping(key, true).Add(local);
        }
    }
    return lookup;
}

Interesting part here is skipping keys which are null. That's why without providing default value you have only one match.


Looks like I found the reason of such behavior. Lookup uses default EqualityComparer, which will return 0 both for key which is null and key which is 0:

int? keyA = 0;
var comparer = EqualityComparer<int?>.Default;
int hashA = comparer.GetHashCode(keyA) & 0x7fffffff; // from Lookup class
int? keyB = null;
int hashB = comparer.GetHashCode(keyB) & 0x7fffffff;
Console.WriteLine(hashA); // 0
Console.WriteLine(hashB); // 0

Possibly nulls skipped to avoid matching null and 0 keys.

5
  • Did I miss that in the documentation anywhere?
    – ryancerium
    Jun 13, 2013 at 16:53
  • @ryancerium just checked msdn, looks like there is no mention of this behavior. But I'm looking on sources with Reflector and see this implementation.. Jun 13, 2013 at 16:53
  • That seems like a fairly serious omission in the documentation to me. Thanks for looking into that for me.
    – ryancerium
    Jun 13, 2013 at 17:01
  • I doubt that is the reason for this behavior. What else would a null value hash to? If the values compared were null, the hash wouldn't even be considered anyway. Jun 13, 2013 at 17:21
  • 1
    @JeffMercado maybe to 42 :) Anyway, they even give no chance to treat null keys manually. If you will pass your own comparer, it even will not be called for null keys (see code above). I think Microsoft should add remark about this behavior Jun 13, 2013 at 17:26
0

I think it's done that way to match the behavior of databases where you can't join on null keys, they are just ignored. There are workarounds to get past this limitation which unfortunately can't be written in LINQ.

You'll have to write your query in such a way that none of the keys are actually null. You can do that simply by wrapping the value in another object that can be compared for equality (e.g., a tuple or anonymous object).

//Join with null elements
var c = a.Join( b,
    dtA => Tuple.Create(dtA.Id),
    dtB => Tuple.Create(dtB.Id),
    (dtA, dtB) => new { aData = dtA.Data, bData = dtB.Data } ).ToList();
1
  • Oops, I guess I misread your question. I thought you were asking why it behaved that way, not why the LINQ-to-Objects implementation behaved like LINQ-to-Entities/-SQL implementations. The answer for that, consistency. Jun 13, 2013 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.