Posted by : Anwar Hossain
Category : How to use LINQ in ASP.NET C#

How to join two collections data using LINQ in ASP.NET C#

Dear views I will show how to join two classes’ data using LINQ in ASP.NET C#. At first I have created two collection one product collection and another order collection .The two collections has a relation that means relational database concept. In this case only matching data will be pulled from data collections using join and equal keyword. We must remember real time data will be pulled from data base.

LINQ JOIN Example in ASP.NET C#

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Linq Join Example</title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
  <asp:GridView ID="gvJoin" runat="server" AutoGenerateColumns="False" CellPadding="4"
  GridLines="None" Width="40%" ForeColor="#333333">
  <AlternatingRowStyle BackColor="White" />
  <Columns>
  <asp:BoundField HeaderText="Product Name" DataField="ProductName" ItemStyle-HorizontalAlign="Center">
  <ItemStyle HorizontalAlign="Center"></ItemStyle>
  </asp:BoundField>
  <asp:BoundField HeaderText="Postal Code" DataField="ShipPostalCode" ItemStyle-HorizontalAlign="Center">
  <ItemStyle HorizontalAlign="Center"></ItemStyle>
   </asp:BoundField>
  <asp:BoundField HeaderText="Destination City" DataField="shipCity" ItemStyle-HorizontalAlign="Center">
  <ItemStyle HorizontalAlign="Center"></ItemStyle>
  </asp:BoundField>
  </Columns>
  <EditRowStyle BackColor="#2461BF" />
  <FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
  <HeaderStyle BackColor="#0099FF" Font-Bold="True" ForeColor="White" />
  <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
  <RowStyle BackColor="#EFF3FB" />
  <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
  <SortedAscendingCellStyle BackColor="#F5F7FB" />
  <SortedAscendingHeaderStyle BackColor="#6D95E1" />
  <SortedDescendingCellStyle BackColor="#E9EBEF" />
  <SortedDescendingHeaderStyle BackColor="#4870BE" />
  </asp:GridView>
  </div>
  </form>
</body>
</html>
 

CODE BEHIND

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace WebApplication1
{
  public partial class innerjoinLinq : System.Web.UI.Page
  {
  protected void Page_Load(object sender, EventArgs e)
  {
  if (!IsPostBack)
  {
 
  var orderForProducts = from Pro in GetProductList()
   join OrderData in GetOrderList()
  on Pro.ProductID equals OrderData.ProductID
  select new
  {
   
  Pro.ProductName, 
  OrderData.ShipPostalCode,
  OrderData.shipCity
 
  };
 
  gvJoin.DataSource = orderForProducts;
  gvJoin.DataBind();
 
 
  }
 
  }
 
  private List<Product>GetProductList()
  {
  List<Product> ProductList = new List<Product>
  {
  new Product{ProductID=1, ProductName="Shirt"},
  new Product{ProductID=2, ProductName="Pant"},
  new Product{ProductID=3, ProductName="Towel"},
  new Product{ProductID=4, ProductName="Fabrics"},
  new Product{ProductID=5, ProductName="Spary"}
  };
 
  return ProductList;
 
  }
 
  private  List<Order> GetOrderList()
  {
 
  List<Order> ProductOrders = new List<Order>{
  new Order{OrderID=1, ProductID=1, ShipPostalCode="1210",shipCity ="Dhaka"},
  new Order{OrderID=2, ProductID=5, ShipPostalCode="2201",shipCity ="Chittagong"},
  new Order{OrderID=3, ProductID=1, ShipPostalCode="1050",shipCity ="Dinajpur"},
   new Order{OrderID=4, ProductID=3, ShipPostalCode="1250",shipCity ="Sylhet"},
  new Order{OrderID=5, ProductID=3, ShipPostalCode="1150",shipCity ="Barisal"},
  new Order{OrderID=6, ProductID=4, ShipPostalCode="1210",shipCity ="Rangpur"}
  };
 
  return ProductOrders;
 
  }
 
  public class Product
  {
  public int ProductID { get; set; }
  public string ProductName { get; set; }
  }
 
  public class Order
  {
  public int OrderID { get; set; }
  public int ProductID { get; set; }
  public string ShipPostalCode { get; set; }
  public string shipCity { get; set; }
  }
  }
}

Out Put

LinqJoinExample-asp.net using-c#



Realted Article Headline

LINQ Average Example using ASP.NET C#
LINQ Sum Example using ASP.NET C#
LINQ Find Minimum value using ASP.NET C#
LINQ Find Max value example using ASP.NET C#
LINQ Count Row example using ASP.NET C#
LINQ Sorting Example using ASP.NET C#
LINQ Equals Example using ASP.NET C#
LINQ Where condition Example using ASP.NET C#
How to skip row in LINQ using ASP.NET C#
How to select Top row in LINQ using ASP.NET C#
How to use SQL like Operator in LINQ using ASP.NET C#
How to join two collections data using LINQ in ASP.NET C#

Article Category

How to create asp.net control dynamically
Learn HTML for beginner
DataList example in C Sharp
Mail sending in asp.net c#
State Management in ASP C #
Basic sql tutorial for Beginner
DataTable example in ASP.Net C#
How to use LINQ in ASP.NET C#
asp.net c # basic tutorial
How to use ajax toolkit in asp.net C#
How to use different types of validation control using asp.net c#
How to use grid view in asp.net c#
Protected by Copyscape Online Plagiarism Detection