Order in join clause matters in Linq

Yesterday I was writing a Linq query that required joining two tables and mistakenly reversed the order of columns being joined. Surely enough compiler complained. I was pretty surprised why my code did not compile. I am sure many of you are SQL gurus and must have written joins and never cared that left column in the join clause must always come from the left table and right column in the join clause must come from right table. SQL server and many other database engines are pretty lenient in it. However Linq flavor requires your key selectors in the join clause must be in proper order. Basically left key of the key selector must come from left sequence or outer sequence. The right key of the key selector should come from right or inner sequence. Here is an example which C# compiler does not like because the order of the key selector is reversed.

image

 

My above query does not compile because my left side on the key selector contains ord.CustomerID which happens to be the column from the right sequence. Once we reverse the order of our keys in the selector, the query compiles fine. Here is a working version of the query.

image

2 Comments

Comments have been disabled for this content.