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 xmlns="">
<head runat="server">
  <title>Linq Count Example</title>
  <form id="form1" runat="server">
  <asp:GridView ID="gvCount" runat="server" AutoGenerateColumns="False" CellPadding="4"
  GridLines="None" Width="40%" ForeColor="#333333">
  <AlternatingRowStyle BackColor="White" />
  <asp:BoundField HeaderText="Prduct Name" DataField="PrductName" ItemStyle-HorizontalAlign="Center">
  <ItemStyle HorizontalAlign="Center"></ItemStyle>
  <asp:BoundField HeaderText="Total Order" DataField="TotalOrder" ItemStyle-HorizontalAlign="Center">
  <ItemStyle HorizontalAlign="Center"></ItemStyle>
  <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" />


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
  } into g
  select new
  PrductName = g.Key.ProductName,
  TotalOrder = g.Count()
  gvCount.DataSource = orderForProducts;
  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


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 control dynamically
Learn HTML for beginner
DataList example in C Sharp
Mail sending in 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# c # basic tutorial
How to use ajax toolkit in C#
How to use different types of validation control using c#
How to use grid view in c#
Protected by Copyscape Online Plagiarism Detection