Join with Into Clause

Joins in linq to SQL are not any different than SQL server. Join basically joins the results of two or more table and shows only record that match in the outer sequence  that has rows present in the inner sequence. Here is an example that illustrates that.

image

image

In the above query, we are joining products with category and than grouping the results by category name. By default join returns only categories that have products so when you apply grouping, you only get categories that have products. What if you want to get categories regardless if they have products? well, you can apply the into clause in linq to SQL which simply projects the results into a temporary variable. However in the background, into clause performs a left outer join so you can get categories that no have products as well. Here is an example.

image

image

In the above output, we also have a category called test cat which has no products. This means that if you want to perform left join and show every item in the outer sequence, you can apply into clause that simply projects the results into a temp variable.

No Comments