How to join table with lamda expression in linq

Posted by Jitendrasoft09 under C# on 6/26/2013 | Points: 10 | Views : 3117 | Status : [Member] [MVP] | Replies : 1
Hi All,
I want to understand How to join table with lamda expression in linq, let me know if anybody knows.

Jitendra Kumar
If my post helps you, plz mark as an answer.



Responses

Posted by: Satyapriyanayak on: 6/27/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Consider 3 tables tblEmployee, tblSalary, tblDepartment.

Here goes the query for grabbing employee name, salary and department name by joining above said three table using lambda.

Initially I am joining tblEmployee and tblSalary. Here sal is the object of table tblSalary and emp is the object of table tblEmployee. Then we are joining the last table tblDepartment. Finally we are filtering the record by Where condition.

IQueryable<Employee> val = DBContext.tblEmployee.Join(DBContext.tblSalary, emp => emp.id, sal => sal.Employee_ID, (emp, sal) => new
{
emp.Name,
emp.id,
sal.Basic_amount,
sal.TDA_Allowance,
sal.House_Rent
})
.Join(DBContext.tblDepartment, emp => emp.id, depart => depart.Employee_id, (emp, depart) => new Employee
{
Name = emp.Name,

Salary = emp.Basic_amount + emp.TDA_Allowance + emp.House_Rent,

Department = depart.Depart_Name

})
.Where(a => a.Name.Contains("Josh") && a.Salary >= 10000.00 );

To convert IQueryable to list :

List MyList = new List();

MyList = val.ToList();



public class Employee

{

public string Name{ get; set; };

public double Salary{ get; set; };

public string Department{ get; set; };

}

http://snippetsofjosh.wordpress.com/2012/05/12/join-multiple-tables-using-lambda-expression/
http://www.codeproject.com/Articles/33769/Basics-of-LINQ-Lamda-Expressions


If this post helps you mark it as answer
Thanks

Jitendrasoft09, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response