Search code examples
linq

Reason of equals keyword in LINQ's join statement


Here is a LINQ query:

from a in db.Table1
join b in db.Table2 on a.Id1 equals b.Id2

I am wondering the reason of equals keyword.

Why did the LINQ creators have to create this keyword? Couldn't they work with ==?

Is there a particular reason ?


Solution

  • As per Microsoft's docs:

    A join clause performs an equijoin. In other words, you can only base matches on the equality of two keys. Other types of comparisons such as "greater than" or "not equals" are not supported. To make clear that all joins are equijoins, the join clause uses the equals keyword instead of the == operator.

    That's the main reason. There are also some other difference between equals and == to make the operator better suitable for LINQ. One important difference is that the operator isn't symmetric. You have to write

    join b in db.Table2 on a.Id1 equals b.Id2
    

    Using

    join b in db.Table2 on b.Id1 equals a.Id2
    

    throws a compiler error.

    Not mentioned (but implicated by the join being an equijoin): another difference is that the operator can't be chained. These join expressions are invalid:

    join b in db.Table2 on a.Id equals b.Id || a.Id equals b.Id2
    
    join b in db.Table2 on a.Id equals b.Id && a.Id2 equals b.Id2
    

    The latter join can be coded by using a composite key.

    I think it was a great decision to use a special contextual keyword. It makes developers pay attention and doesn't raise expectations that an == operator might have done.