Showing posts with label Lambda JOINS. Show all posts
Showing posts with label Lambda JOINS. Show all posts

Friday, October 18, 2013

Lambda Expressions with JOIN in C#

Let us consider that we have two tables 
1.Login[Username,Password,ID as fields] and 
2.Salary[ID,Salary as Fields]

Open MS Visual Studio--> select ASP.Net empty website
Add--> 2 Gridviews and 1 Button
Add--> Linq to SQL from Data and give the name as TestOpportunity
Add the SQL Connection and then Drag and Drop the Tables[Login and Salary]

write the below code on Button1_click
TestOpportunityDataContext TODC = new TestOpportunityDataContext();
//var jointables = TODC.logins.Where(r=>r.ID>=3).Select(r => new {r.username,r.password,r.ID }).Where(r=>r.username.Contains("Sai"));
var  result= TODC.logins.Select(r => new { r.username, r.password, r.ID }).Where(r => r.username.Contains("Sai")).Distinct();
GridView1.DataSource = result;
GridView1.DataBind();

var jointables= TODC.logins.Join(TODC.Salaries, b => b.ID, c => c.ID, (b, c) => new { b.username,b.password,c.Salary1});
GridView2.DataSource = jointables;
GridView2.DataBind();