Sunday, August 25, 2013

How to use Join in LINQ Query

Introduction: In this post I have try to explain how we can use the Joins in LINQ.
LINQ

Description:
LINQ_TABLE
LINQ

LINQ_STATE (ID is primary key)
ID
int
STATE
varchar(50)

Add a web form to application. Go to Solution Explorer, right click on website>Add new item> Web from.
Drag and drop a Gridview Data control from from Toolbox>Data.


<asp:GridView ID="grduser" runat="server" AutoGenerateColumns="False"
            AllowPaging="True" onpageindexchanging="grduser_PageIndexChanging"
            >
            <Columns>
                <asp:TemplateField HeaderText="Username">
                                      <ItemTemplate>
                        <asp:Label ID="lbluser" runat="server" Text='<%# Eval("USERNAME") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="First Name">
                    <ItemTemplate>
                        <asp:Label ID="lblfirst" runat="server" Text='<%# Eval("FIRST_NAME") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Last Name">
                    <ItemTemplate>
                        <asp:Label ID="lbllast" runat="server" Text='<%# Eval("LAST_NAME") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Date Of Birth">
                    <ItemTemplate>
                        <asp:Label ID="lblbirth" runat="server" Text='<%# Eval("DATE_BIRTH") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Sex">
                    <ItemTemplate>
                        <asp:Label ID="lblsex" runat="server" Text='<%# Eval("SEX") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Age">
                    <ItemTemplate>
                        <asp:Label ID="lblage" runat="server" Text='<%# Eval("AGE") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Profile Image">
                    <ItemTemplate>
                        <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("IMAGE") %>' Height="100px" Width="100px" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="State">
                <ItemTemplate>
                <asp:Label ID="lbldepartment" runat="server" Text='<%#Eval("STATE") %>'></asp:Label>
                </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

Now go to .aspx.cs page.
DataClassesDataContext db = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bindgrid();
        }
    }

    private void Bindgrid()
    {
        //inner join
        var bind = from v in db.LINQ_TABLEs
                   join d in db.LINQ_STATEs on v.ID equals d.ID
                   select new
                   {
                       v.USERNAME,
                       v.FIRST_NAME,
                       v.LAST_NAME,
                       v.DATE_BIRTH,
                       v.SEX,
                       v.AGE,
                       v.IMAGE,
                       d.STATE
                   };
grduser.DataSource = bind;
        grduser.DataBind();
    }
    protected void grduser_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grduser.PageIndex = e.NewPageIndex;
        Bindgrid();
    }

In VB

Private db As New DataClassesDataContext()
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Bindgrid()
        End If
    End Sub
    Private Sub Bindgrid()
        'inner join
        Dim bind = From v In db.LINQ_TABLEs Join d In db.LINQ_STATEs
                  On v.ID Equals d.ID
                   Select v.USERNAME, v.FIRST_NAME, v.LAST_NAME, v.DATE_BIRTH,
                   v.SEX, v.AGE, v.IMAGE, d.STATE
        grduser.DataSource = bind
        grduser.DataBind()
    End Sub
    Protected Sub grduser_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
        grduser.PageIndex = e.NewPageIndex
        Bindgrid()
    End Sub

Now run the project and check the result. 

No comments:

Post a Comment