LINQ to SQL Inner Join

Input tables

Department table
departments


Employees table
employees


Expenses table
expenses


Employee Expenses table
emp-expenses


LINQ Query:

Below LINQ query will get the data from tblEmployees table and tblDepartments table by inner join.

MyDBDataContext sqlObj = new MyDBDataContext(); var employees = from emps in sqlObj.tblEmployees join depts in sqlObj.tblDepartments on emps.DepartmentID equals depts.DepartmentID select new { emps.EmployeeID, emps.EmployeeName, emps.Salary, depts.DepartmentName }; gvemployees.DataSource = employees; gvemployees.DataBind();

Output:

inner-join

LINQ Query for inner join with multiple table:

Below LINQ query will get data from multiple tables by using inner join.

MyDBDataContext sqlObj = new MyDBDataContext(); var employees = from emps in sqlObj.tblEmployees join depts in sqlObj.tblDepartments on emps.DepartmentID equals depts.DepartmentID join empexp in sqlObj.tblEmployeeExpenses on emps.EmployeeID equals empexp.EmployeeID join exp in sqlObj.tblExpenses on empexp.ExpenseID equals exp.ExpenseID select new { emps.EmployeeID, emps.EmployeeName, emps.Salary, depts.DepartmentName, exp.Expense, ExpenseAmount = empexp.Amount, ExpenseDate = empexp.ExpenseDate.ToShortDateString() }; gvemployees.DataSource = employees; gvemployees.DataBind();

Output:

inner-join-multiple-tables