Basics of LINQ
Join with Lamda Expressions(Inner Join with Linq)...................
Note: In this topic, we will discuss about join with Linq
Concept ……….
--Execute this query in Sql Server…………………………
create database LinqwithJoin
use LinqwithJoin
--Code for Create table
create table StuRecord (id int,Name nvarchar(50),City nvarchar(50))
create table StuCollege(id int,CollegeName nvarchar(50),BranchName nvarchar(50))
-- insert record both table.................
insert into StuRecord values(1,'Kush Tiwari','Ghazipur')
insert into StuCollege values(1,'Sms Varanasi','M.C.A')
-- Join or Inner Join in Sql Server
Select StuRecord.Id,StuRecord.Name,StuRecord.City,StuCollege.CollegeName,StuCollege.BranchName
from StuRecord inner join StuCollege
on StuRecord.id=StuCollege.id
Select r.Id,r.Name,r.City,c.CollegeName,c.BranchName from
StuRecord r join
StuCollege c on r.id=c.id
//Inner Join/Join with Linq Concept using C# .Net..............
//Code for Default.aspx……………………………………….
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="_Default"
%>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td colspan="2">
<center> Basics of LINQ Join with Lamda Expressions</center>
</td>
</tr>
<tr>
<td colspan="2"><center>
<asp:Button ID="Button1"
runat="server"
onclick="Button1_Click"
Text="Inner Join With Linq
Concept" /></center>
</td>
</tr>
<tr>
<td colspan="2">
<center> <asp:GridView ID="GridView1" runat="server" CellPadding="4"
ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White"
/>
<FooterStyle BackColor="#990000"
Font-Bold="True"
ForeColor="White"
/>
<HeaderStyle BackColor="#990000"
Font-Bold="True"
ForeColor="White"
/>
<PagerStyle BackColor="#FFCC66"
ForeColor="#333333"
HorizontalAlign="Center"
/>
<RowStyle BackColor="#FFFBD6"
ForeColor="#333333"
/>
<SelectedRowStyle BackColor="#FFCC66"
Font-Bold="True"
ForeColor="Navy"
/>
<SortedAscendingCellStyle BackColor="#FDF5AC"
/>
<SortedAscendingHeaderStyle BackColor="#4D0000"
/>
<SortedDescendingCellStyle BackColor="#FCF6C0"
/>
<SortedDescendingHeaderStyle BackColor="#820000"
/>
</asp:GridView></center>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
//Code for Default.aspx.cs……………………………………….
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default :
System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs
e)
{
}
DataClassesDataContext dd = new DataClassesDataContext();
protected void
Button1_Click(object sender, EventArgs e)
{
// Linq inner join with Old Format…………………………………
var v = from m in dd.StuRecords
from w in
dd.StuColleges
where m.id == w.id
select new { m.id,
m.Name, m.City, w.BranchName, w.CollegeName };
// Linq inner join with New Format………
var v = from m in dd.StuRecords
join s in
dd.StuColleges
on m.id equals
s.id
select new { m.id,
m.Name, m.City, s.BranchName, s.CollegeName };
//Using Linq Lamda Expressions…………………
var v = dd.StuRecords.Join(dd.StuColleges, x =>
x.id, z => z.id, (x, z) => new { x.id,
x.Name, x.City, z.BranchName, z.CollegeName }).ToList();
GridView1.DataSource
= v;
GridView1.DataBind();
}
0 comments:
Post a Comment