Joins are the most important function of SQL Operators. Linq supports join operations using the join operator. The join operator performs an inner join of two collections based on matching keys extracted from the elements.
Let See how to create a Join.
Step 1
Create a web application and give the solution name as SolLinqJoin.
Step 2
Add a gridview in page,it is look like this
<%@ 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> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GvStudent" runat="server" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" EnableModelValidation="True" ForeColor="Black" GridLines="Vertical"> <AlternatingRowStyle BackColor="#CCCCCC" /> <FooterStyle BackColor="#CCCCCC" /> <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" /> </asp:GridView> </div> </form> </body> </html>
Step 3
Create a Division class in App_Code folder,it is look like this
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Add the Division Data /// </summary> public class Division { #region Property public int DivID { get; set; } public String DivName { get; set; } #endregion }
Step 4
Create a Student class in App_Code folder,it is look like this
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Add a Student Data /// </summary> public class Student { #region Property public int StudentID { get; set; } public String Name { get; set; } public int DivID { get; set; } #endregion }
Step 5
Create a method for add division and student data in default.cs class,it is look like this
/// <summary> /// Add the Division Data /// </summary> /// <returns>List</returns> private List<Division> AddDivisionData() { try { Division Div1 = new Division(); Div1.DivID = 1; Div1.DivName = "A"; Division Div2 = new Division(); Div2.DivID = 2; Div2.DivName = "B"; Division Div3 = new Division(); Div3.DivID=3; Div3.DivName="C"; List<Division> ListDiv = new List<Division>(); ListDiv.Add(Div1); ListDiv.Add(Div2); ListDiv.Add(Div3); return ListDiv; } catch (Exception ex) { throw new Exception(ex.Message); } } /// <summary> /// Add the Student Data /// </summary> /// <returns>List</returns> private List<Student> AddStudentData() { try { Student Std1 = new Student(); Std1.StudentID = 1; Std1.Name = "Kishor"; Std1.DivID = 1; Student Std2 = new Student(); Std2.StudentID = 2; Std2.Name = "Kakashi"; Std2.DivID = 1; Student Std3 = new Student(); Std3.StudentID = 3; Std3.Name = "Lee"; Std3.DivID = 2; Student Std4 = new Student(); Std4.StudentID = 4; Std4.Name = "Ramdas"; Std4.DivID = 1; Student Std5 = new Student(); Std5.StudentID = 5; Std5.Name = "Jhon"; Std5.DivID = 2; Student Std6 = new Student(); Std6.StudentID = 6; Std6.Name = "Jiraiya"; Std6.DivID = 3; Student Std7 = new Student(); Std7.StudentID = 7; Std7.Name = "shikamaro"; Std7.DivID = 3; Student Std8 = new Student(); Std8.StudentID = 8; Std8.Name = "Eun-ju"; Std8.DivID = 1; Student Std9 = new Student(); Std9.StudentID = 9; Std9.Name = "Naruto"; Std9.DivID = 3; Student Std10 = new Student(); Std10.StudentID = 10; Std10.Name = "David"; Std10.DivID = 2; Student Std11 = new Student(); Std11.StudentID = 11; Std11.Name = "Sakura"; Std11.DivID = 2; List<Student> StudentObj = new List<Student>(); StudentObj.Add(Std1); StudentObj.Add(Std2); StudentObj.Add(Std3); StudentObj.Add(Std4); StudentObj.Add(Std5); StudentObj.Add(Std6); StudentObj.Add(Std7); StudentObj.Add(Std8); StudentObj.Add(Std9); StudentObj.Add(Std10); StudentObj.Add(Std11); return StudentObj; } catch (Exception ex) { throw new Exception(ex.Message); } }
Step 6
Perform join operation,it is look like this
/// <summary> /// Perform join operation and display data in gridview /// </summary> /// <param name="ListDiv"></param> /// <param name="ListStudent"></param> private void Join(List<Division>ListDiv,List<Student>ListStudent) { try { /// get Division Data var DivQuery = from D in ListDiv select D; /// Get Student Data var StudentQuery = from S in ListStudent select S; /// Join Process var JoinQuery = from D in DivQuery join S in StudentQuery on D.DivID equals S.DivID orderby S.StudentID ascending select new { StudentID=S.StudentID, Name=S.Name, Division=D.DivName }; /// Bind the Data in GridView GvStudent.DataSource = JoinQuery; GvStudent.DataBind(); } catch (Exception ex) { throw new Exception(ex.Message); } }
Step 7
Call a Join method in Page_Load event, it is look like this
protected void Page_Load(object sender, EventArgs e) { try { if (IsPostBack == false) { Join(AddDivisionData(), AddStudentData()); } } catch (Exception) { } }
Full .aspx.cs Code
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) { try { if (IsPostBack == false) { Join(AddDivisionData(), AddStudentData()); } } catch (Exception) { } } #region Methods /// <summary> /// Add the Division Data /// </summary> /// <returns>List</returns> private List<Division> AddDivisionData() { try { Division Div1 = new Division(); Div1.DivID = 1; Div1.DivName = "A"; Division Div2 = new Division(); Div2.DivID = 2; Div2.DivName = "B"; Division Div3 = new Division(); Div3.DivID=3; Div3.DivName="C"; List<Division> ListDiv = new List<Division>(); ListDiv.Add(Div1); ListDiv.Add(Div2); ListDiv.Add(Div3); return ListDiv; } catch (Exception ex) { throw new Exception(ex.Message); } } /// <summary> /// Add the Student Data /// </summary> /// <returns>List</returns> private List<Student> AddStudentData() { try { Student Std1 = new Student(); Std1.StudentID = 1; Std1.Name = "Kishor"; Std1.DivID = 1; Student Std2 = new Student(); Std2.StudentID = 2; Std2.Name = "Kakashi"; Std2.DivID = 1; Student Std3 = new Student(); Std3.StudentID = 3; Std3.Name = "Lee"; Std3.DivID = 2; Student Std4 = new Student(); Std4.StudentID = 4; Std4.Name = "Ramdas"; Std4.DivID = 1; Student Std5 = new Student(); Std5.StudentID = 5; Std5.Name = "Jhon"; Std5.DivID = 2; Student Std6 = new Student(); Std6.StudentID = 6; Std6.Name = "Jiraiya"; Std6.DivID = 3; Student Std7 = new Student(); Std7.StudentID = 7; Std7.Name = "shikamaro"; Std7.DivID = 3; Student Std8 = new Student(); Std8.StudentID = 8; Std8.Name = "Eun-ju"; Std8.DivID = 1; Student Std9 = new Student(); Std9.StudentID = 9; Std9.Name = "Naruto"; Std9.DivID = 3; Student Std10 = new Student(); Std10.StudentID = 10; Std10.Name = "David"; Std10.DivID = 2; Student Std11 = new Student(); Std11.StudentID = 11; Std11.Name = "Sakura"; Std11.DivID = 2; List<Student> StudentObj = new List<Student>(); StudentObj.Add(Std1); StudentObj.Add(Std2); StudentObj.Add(Std3); StudentObj.Add(Std4); StudentObj.Add(Std5); StudentObj.Add(Std6); StudentObj.Add(Std7); StudentObj.Add(Std8); StudentObj.Add(Std9); StudentObj.Add(Std10); StudentObj.Add(Std11); return StudentObj; } catch (Exception ex) { throw new Exception(ex.Message); } } /// <summary> /// Perform join operation and display data in gridview /// </summary> /// <param name="ListDiv"></param> /// <param name="ListStudent"></param> private void Join(List<Division>ListDiv,List<Student>ListStudent) { try { /// get Division Data var DivQuery = from D in ListDiv select D; /// Get Student Data var StudentQuery = from S in ListStudent select S; /// Join Process var JoinQuery = from D in DivQuery join S in StudentQuery on D.DivID equals S.DivID orderby S.StudentID ascending select new { StudentID=S.StudentID, Name=S.Name, Division=D.DivName }; /// Bind the Data in GridView GvStudent.DataSource = JoinQuery; GvStudent.DataBind(); } catch (Exception ex) { throw new Exception(ex.Message); } } #endregion }
No comments:
Post a Comment