LINQ to SQL Group Join

In this example we will write the query to get the Employees and their total sum of expense amount using LINQ to SQL Group join.

Input tables

Employees table
employees


Employee Expenses table
emp-expenses


LINQ Query:

MyDBDataContext sqlObj = new MyDBDataContext(); var employees = from emps in sqlObj.tblEmployees join empexp in sqlObj.tblEmployeeExpenses on emps.EmployeeID equals empexp.EmployeeID into g select new { emps.EmployeeID, emps.EmployeeName, emps.Salary, ExpenseAmount = g.Sum(x => (decimal?)x.Amount) }; gvemployees.DataSource = employees; gvemployees.DataBind();

Output:

group-join