In LINQ to SQL, the LEFT JOIN is useful to return all the records or rows from the left table and matching records from the right table. If no columns matching rows in the right table, it will return only left table records.
In LINQ to SQL, to achieve LEFT JOIN behavior, it's mandatory to use the "INTO" keyword and "DefaultIfEmpty()" method.
Following is the syntax of using LINQ to SQL Left Outer Join to get data from multiple tables.
If you observe the above syntax, we joined “EmployeeDetails”, “Departments” tables using into and DefaultIfEmpty() methods to get required data by using left outer join.
Before we start implementing LINQ to SQL left outer join example, first, we need to create a database with required tables and map those tables to LINQ to SQL file (.dbml). If you don't know the process, don't worry; check this link create and map database tables to LINQ to SQL file (.dbml).
Once we create and map required tables to the .dbml file now, we will show data in our application for that Right-click on the application à select Add à New Item à Select Web Form à Give name as Default.aspx and click the OK button.
Now open the Default.aspx page and write the code as shown below.
Now open the code behind file and write the code as shown below.
If you observe the above example, we are trying to get data from the “EmployeeDetails” and “Departments” table using the left outer join.
Following is the result of LINQ to SQL left outer join example.
This is how we can use LINQ to SQL left outer join in c# to get the required data from multiple tables based on our requirements.