Why doesn't link simply use the == operator in joins if t can use it in a 'where' clause?
In a LINQ query expression, join operations are performed on object collections. Object collections cannot be "joined" in exactly the same way as two relational tables. In LINQ, explicit join clauses are only required when two source sequences are not tied by any relationship.
Cross join is also known as full join.
In LINQ, an inner join is used to serve a result which contains only those elements from the first data source that appears only one time in the second data source. And if an element of the first data source does not have matching elements, then it will not appear in the result data set.
Join Method (System. Linq) Correlates the elements of two sequences based on matching keys.
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.
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