What is Order By

To sort data in LINQ we use orderby in the following manner:-

Sorting with Array

		
	int[] marks = new int[] { 23, 45, 78, 90, 56, 89, 10, 32 };

        // it will sort in ascending order
        var sort_asc = from res_asc in marks
                       orderby res_asc
                       select res_asc;

        //it will sort in descending order
        var sort_desc = from res_desc in marks
                        orderby res_desc descending
                        select res_desc;

        Response.Write("Ascending order result <br/>");
        foreach (int res_asc in sort_asc)
        {

            Response.Write(res_asc + " ");
       
        }


        Response.Write("
descending order result <br/>"); foreach (int res_desc in sort_desc) { Response.Write(res_desc + " "); }

The output of the code is as follows:-

		
		sorting with array

		Figure 1
		
		

Sorting with Generics

		
	List<data> dt = new List<data>()
        {
            new data{roll_no=1, student="isha", per=100},
            new data{roll_no=2, student="sneha", per=89},
            new data{roll_no=3, student="rahul", per=34},
            new data{roll_no=4, student="renu", per=34},
            new data{roll_no=5, student="sapna", per=89}
        };


        var res = from res_sort_asc in dt
                  orderby res_sort_asc.per
                  select res_sort_asc;
       
        Response.Write("Ascending order on the basis of Per<br/>");
       
        foreach (data res_after_sort in res)
        {
       
            Response.Write("Roll No:- "+res_after_sort.roll_no+" Student:- "+res_after_sort.student+"  per:- "+res_after_sort.per+"<br/>");
       
        }

		
		

The output of this code as follows:-

		
		sorting with genrics

		Figure 2
		
		

Similarly if we want to perform descending ordering then we will use following code:-

		
		var res = from res_sort_asc in dt
                  orderby res_sort_asc.per descending
                  select res_sort_asc;
	
		

Sort by more than one column

When we want to sort on the basis of more than one column then we will use following code:-

		
		List<data> dt = new List<data>()
        {
            new data{roll_no=1, student="isha", per=100},
            new data{roll_no=2, student="sneha", per=89},
            new data{roll_no=3, student="rahul", per=34},
            new data{roll_no=4, student="renu", per=34},
            new data{roll_no=5, student="sapna", per=89}
        };

        //sort first on the basis of per then by student
        var res = from res_sort_asc in dt
                  orderby res_sort_asc.per, res_sort_asc.student
                  select res_sort_asc;
       
        Response.Write("Ascending order on the basis of Per and student<br/>");
       
        foreach (data res_after_sort in res)
        {
       
            Response.Write("Roll No:- "+res_after_sort.roll_no+" Student:- "+res_after_sort.student+"  per:- "+res_after_sort.per+"<br/>");
       
        }



		
		
		sort by more than one column

		Figure 3

		
		

Sorting with Database

If we want to sort data on the basis of any column then we use the following code in LINQ:-

		
		DataContext dc = new DataContext("Data Source=.;Initial Catalog=TechAltum;Integrated Security=True");

       
        var res = dc.GetTable<Class1>().OrderBy(x => x.no_of_prod);
       
        //sorting on the basis of no of pord
       
        foreach (Class1 res_data in res)
        {
       
            Response.Write("Prod Year="+res_data.prod_year+", Dept="+res_data.dept+", No_of_prod="+res_data.no_of_prod+"<br/>");
        }


		
		
		sorting with database

		Figure 4

		
		

Sorting in descending order:-

		
		var res = dc.GetTable<Class1>().OrderByDescending(x => x.no_of_prod);
		
		

sorting on the basis of more than one columne:-

		
		var res = dc.GetTable<Class1>().OrderByDescending(x => x.no_of_prod).ThenBy(x => x.prod_year);
		
		

Email Address

For any query you can send mail at info@techaltum.com
Thanks