User Id :    Password :      New Member   Forgot Password  
 
How to sort a data table in C# with LINQ
Description One of friend today ask how we can sort data table based on particular column? So I thought it’s a good idea to write a blog post about it. In this blog post we learn how we can learn how we can sort database with LINQ queries without writing much more long code.   No. of Views     1828
  Rating     0
Author Jalpesh Vadgama   Posted On     30 May 2013
Tags C#,LINQ,C# 4.0    

Sample Code   Download Code

 

One of friend today ask how we can sort data table based on particular column? So I thought it’s a good idea to write a blog post about it. In this blog post we learn how we can learn how we can sort database with LINQ queries without writing much more long code. So let’s write a code for that.

 

using System;
using System.Data;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dataTable = CreateDataTalble();
            AddRowToDataTable(dataTable);
            Console.WriteLine("Before sorting");
            PrintDatable(dataTable);
            var Rows = (from row in dataTable.AsEnumerable()
                        orderby row["FirstName"] descending
                        select row);
            dataTable = Rows.AsDataView().ToTable();
            Console.WriteLine("==============================");
            Console.WriteLine("After sorting");
            PrintDatable(dataTable);
        }
 
        public static DataTable CreateDataTalble()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("FirstName", typeof(string));
            dt.Columns.Add("LastName", typeof(string));
            return dt;
        }
        public static void AddRowToDataTable(DataTable dt)
        {
            dt.Rows.Add("Jalpesh", "Vadgama");
            dt.Rows.Add("Vishal", "Vadgama");
            dt.Rows.Add("Teerth", "Vadgama");
            dt.Rows.Add("Pravin", "Vadgama");
        }
 
        public static void PrintDatable(DataTable dt)
        {
            foreach (DataRow row in dt.Rows)
            {
                Console.WriteLine(string.Format("{0} {1}",
                    row[0], row[1]));
            }
        }
    }
}



So here in the above application , First I have created a datable with two column ‘FirstName’ and ‘LastName’ in CreateDataTable function. Also I have create a function called ‘AddRowToDataTable’
to add some rows. Then I have created a common function to print a datable with Console.Writeline.

Now to short the datable I have used a LINQ feature to convert database as IEnumerable and then I have used ‘OrderBy’ operator to sort IEnumerable with First Name row. It will return a sorted EnumerableRowCollection of rows.

Then I have converted EnumerableRowCollection to dataview and then cast as datable. That’s it. We are done with code. Let’s run this example and see how its works.
 

 


So it’s very easy. Hope you like it. Stay tuned for more updates…

 

About Author

About Author I am jalpesh vadgama an Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having more then 5 years of experience in .NET Technology.Please feel free to contact me for any queries via posting comments on my blog I will try to reply as early as possible. Please also visit my blog at http://jalpesh.blogspot.com/ Jalpesh Vadgama
No Photo
 
Country India
Company N/A
Home Page http://jalpesh.blogspot.com/

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

   
Write your comment here.
Comment
Verification Code