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

LINQ Average Example using ASP.NET C#

Dear viewer I will show how to average row value using LINQ in ASP.NET C#. I have shown a very simple example to average total value from list. I have created a two classes one student and another student Mark to provide temporary data to grid view .In this case student and student-Mark class has a parent and child class relationship to each other. Hear I will find average number each student using LINQ Average() Method.

Find Average Number in LINQ

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <title> LINQ Average Example</title>
  <style type="text/css">
  .style4
  {
  color: red;
  }
  </style>
</head>
<body>
  <form id="form1" runat="server">
  <div>
  <div>
  <table width="100%">
  <tr>
  <td colspan="3">
  <h2 class="style4">
  LINQ Average Example</h2>
  </td>
  </tr>
  <tr>
  <td width="260">
  &nbsp;
  </td>
  <td width="10">
   &nbsp;
  </td>
  <td>
  &nbsp;
  </td>
  </tr>
  <tr>
  <td colspan="3">
  <asp:GridView ID="gvAverager" runat="server" AutoGenerateColumns="False" CellPadding="4"
  ForeColor="#333333" GridLines="None" Width="40%">
  <AlternatingRowStyle BackColor="White" />
  <Columns>
  <asp:BoundField DataField="StudentName" HeaderText="StduentName" ItemStyle-HorizontalAlign="Center">
  <ItemStyle HorizontalAlign="Center" />
  </asp:BoundField>
  <asp:BoundField DataField="AverageNumber" HeaderText="AverageNumber" ItemStyle-HorizontalAlign="Center">
  <ItemStyle HorizontalAlign="Center" />
  </asp:BoundField>
  </Columns>
  <EditRowStyle BackColor="#7C6F57" />
  <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
  <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
  <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
  <RowStyle BackColor="#E3EAEB" />
  <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
  <SortedAscendingCellStyle BackColor="#F8FAFA" />
  <SortedAscendingHeaderStyle BackColor="#246B61" />
  <SortedDescendingCellStyle BackColor="#D4DFE1" />
  <SortedDescendingHeaderStyle BackColor="#15524A" />
  </asp:GridView>
  </td>
  </tr>
  </table>
  </div>
  </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 LinqAverage : System.Web.UI.Page
{
  private void Page_Load(object sender, EventArgs e)
  {
  if (!IsPostBack)
  {
 
 
  var data = from s in GetStudentList()
  join m in GetStudentMarkList() on s.ID equals m.StduentID
  group new { m, s } by new
  {
  s.StduentName
  } into g
 
  select new
  {
  StudentName = g.Key.StduentName,
   AverageNumber = g.Sum(i => i.m.ObtainNumber)
  };
 
  gvAverager.DataSource = data;
  gvAverager.DataBind();
 
 
 
 
 
 
 
 
  }
  }
 
  private List<Student> GetStudentList()
  {
  List<Student> StudentList = new List<Student>
  {
 
  new Student{ID=1, StduentName="Ashikur Rahman"},
  new Student{ID=2, StduentName="Roman Mozomder"},
  new Student{ID=3, StduentName="Iqbal Hossain"},
 
  };
 
  return StudentList;
 
  }
 
  private List<StudentMark> GetStudentMarkList()
  {
 
  List<StudentMark> StudentMarkStudentMarks = new List<StudentMark>{
  new StudentMark{StduentID=1, Subject="English", ObtainNumber =88},
  new StudentMark{StduentID=1, Subject="Math", ObtainNumber =95},
  new StudentMark{StduentID=1, Subject="Accounting", ObtainNumber =65},
  new StudentMark{StduentID=2, Subject="English" , ObtainNumber =75},
  new StudentMark{StduentID=2, Subject="Math" , ObtainNumber =95},
  new StudentMark{StduentID=2, Subject="Accounting" , ObtainNumber =55},
  new StudentMark{StduentID=3, Subject="English" , ObtainNumber =88},
  new StudentMark{StduentID=3, Subject="Math" , ObtainNumber =85},
  new StudentMark{StduentID=3, Subject="Accounting" , ObtainNumber =79}
 
 
 
  };
 
  return StudentMarkStudentMarks;
 
  }
 
  public class StudentMark
   {
  public int StduentID { get; set; }
  public string StduentName { get; set; }
  public string Subject { get; set; }
  public decimal  ObtainNumber { get; set; }
  }
 
  public class Student
  {
  public int ID { get; set; }
  public string StduentName { get; set; }
 
  }
}

Out Put

Linq-Average-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