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

LINQ Sorting Example using ASP.NET C#

Dear viewer I will show how to Sort a list of data using LINQ in ASP.NET C#. I have shown a very simple example to sort a list from a list using LINQ Order by Keywords. I have created a student list to provide virtual data to grid view then a grid view has been taken to display the list of Student. In this case any properties of a list can be used to sort a list of data that can be order ID, Can be order by Name.

Sorting Example in LINQ


<html xmlns="">
<head id="Head1" runat="server">
  <title> LINQ Sorting Example</title>
  <style type="text/css">
  color: #99CC00;
  <form id="form1" runat="server">
  <table width="100%">
  <td colspan="3">
  <h2 class="style4">
  LINQ Sorting Example</h2>
  <td width="260">
  <td width="10">
  <td colspan="3">
  <asp:GridView ID="gvLikeOperator" runat="server" AutoGenerateColumns="False" CellPadding="4"
   ForeColor="#333333" GridLines="None" Width="40%">
  <AlternatingRowStyle BackColor="White" />
  <asp:BoundField DataField="StudentName" HeaderText="Student Name" ItemStyle-HorizontalAlign="Center">
  <ItemStyle HorizontalAlign="Center" />
  <asp:BoundField DataField="Number" HeaderText="Marks" ItemStyle-HorizontalAlign="Center">
  <ItemStyle HorizontalAlign="Center" />
  <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" />


using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq.SqlClient;
public partial class LinqSorting : System.Web.UI.Page
  protected void Page_Load(object sender, EventArgs e)
  if (!IsPostBack)
  var data = (from m in GetStudentList()
  where m.Number> 65
  orderby m.StudentName
  select m);
   gvLikeOperator.DataSource = data;
  protected void btnSubmit_Click(object sender, EventArgs e)
  private List<Student> GetStudentList()
  List<Student> StudentStudents = new List<Student>{
  new Student{StudentID=1,  StudentName="Amin", Number =55},
  new Student{StudentID=2,  StudentName="Alomgir" , Number =55},
  new Student{StudentID=3,  StudentName="Ashikur Rahman", Number =65},
  new Student{StudentID=4,  StudentName="Shamim Ahmed" , Number =55},
  new Student{StudentID=5,  StudentName="Amolash" , Number =95},
  new Student{StudentID=6,  StudentName="Alum" , Number =95},
  new Student{StudentID=7,  StudentName="Abil", Number =75},
  new Student{StudentID=8,  StudentName="Enamul", Number =95},
  new Student{StudentID=9,  StudentName="Nahid", Number =95},
  new Student{StudentID=10,  StudentName="Asraf", Number =95},
  new Student{StudentID=11,  StudentName="Asfaq", Number =85},
  new Student{StudentID=12,  StudentName="kbil", Number =95},
  new Student{StudentID=13,  StudentName="Masum", Number =95},
  new Student{StudentID=14,  StudentName="Nasid", Number =45},
  new Student{StudentID=15,  StudentName="Babu", Number =75},
  new Student{StudentID=16,  StudentName="Titu", Number =85}
  return StudentStudents;
  public class Student
  public int StudentID { get; set; }
  public string StudentName { get; set; }
  public  int  Number{ 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