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

LINQ Count Row example using ASP.NET C#

Dear viewer I will show how to count row using LINQ in ASP.NET C#. I have shown a very simple example to count row. I have created a two class’s one product and another order to provide temporary data to grid view .In this case product and order class has a parent and child relationship to each other. Hear I will find what amount of particular product has been ordered using LINQ Count() Method.

Count Row Example in LINQ

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Linq Count Example</title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
  <asp:GridView ID="gvCount" runat="server" AutoGenerateColumns="False" CellPadding="4"
  GridLines="None" Width="40%" ForeColor="#333333">
  <AlternatingRowStyle BackColor="White" />
  <Columns>
  <asp:BoundField HeaderText="Prduct Name" DataField="PrductName" ItemStyle-HorizontalAlign="Center">
  <ItemStyle HorizontalAlign="Center"></ItemStyle>
  </asp:BoundField>
 
  <asp:BoundField HeaderText="Total Order" DataField="TotalOrder" ItemStyle-HorizontalAlign="Center">
  <ItemStyle HorizontalAlign="Center"></ItemStyle>
  </asp:BoundField>
  </Columns>
  <EditRowStyle BackColor="#2461BF" />
  <FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
  <HeaderStyle BackColor="#CC9900" Font-Bold="True" ForeColor="White" />
  <PagerStyle BackColor="#CCFF99" 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 FILE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class LinqCountExample : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
 
  var orderForProducts = from Pro in GetProductList()
  join OrderData in GetOrderList() on Pro.ProductID equals OrderData.ProductID
  group new { Pro, OrderData } by new
  {
  Pro.ProductName
 
 
  } into g
 
  select new
  {
  PrductName = g.Key.ProductName,
  TotalOrder = g.Count()
 
  };
 
  gvCount.DataSource = orderForProducts;
  gvCount.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="Cap"}
  };
 
  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"},
  new Order{OrderID=3, ProductID=1, ShipPostalCode="1050",shipCity ="Cox'sbazar"},
  };
 
  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

Linq-count-example



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