tags:

views:

122

answers:

2

Why doesn't link simply use the == operator in joins if t can use it in a 'where' clause?

+6  A: 

You would have to ask the designers but it is clear that allowing any (boolean) expression would allow way too much possibilities. Using a special keyword like equals makes it much easier to constrain it to the specification as 2 proper columns.

I found a discussion on The Moth.

Henk Holterman
That makes sense.
ProfK
+9  A: 

The two sides of the equality in a join are treated as two separate lambda expressions which generate the keys for the two sequences.

from category in categories
join prod in products on category.ID equals prod.CategoryID

categories.Join(products,
                category => category.ID,
                prod => prod.CategoryID,
                (category, prod) => new { Category = category, Product=prod });

Using the equals keyword makes it unabmiguous where one lambda ends and the other starts. For a where clause, on the other hand, there's a single lambda expression which decides whether each item matches:

from prod in products
where prod.CategoryID == 1

products.Where( prod => prod.CategoryID == 1 )

In theory, joins could have been implemented with a single lambda as

from category in categories
join prod in products on category.ID == prod.CategoryID

categories.Join(products,
                (category, prod) => category.ID == prod.CategoryID,
                (category, prod) => new { Category = category, Product=prod });

However, by computing two keys and doing the comparison itself, LINQ can use hash tables to compute the join more efficiently than if it had to execute an arbitrary comparison for every pair of elements.

stevemegson