Friday, 14 June 2013

LINQ Hour 5-2 Sorting in LINQ

Written By:-Isha Malhotra 
Our Website:-www.techaltum.com
Sorting in LINQ

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("<br/>descending order result <br/>");
        foreach (int res_desc in sort_desc)
        {

            Response.Write(res_desc + " ");

        }

The output of the code is as follows:-



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:-



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;

Sorting on the basis of 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/>");
       
        }

The output of this code as follows:-



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/>");
        }

The output of this code is as follows:-



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);

Hope you enjoyed the article. For any query you can mail me at info@techaltum.com


16 comments:

  1. Good article. It covers all aspects of ordering

    ReplyDelete
  2. Webtrackker Technology is a leading mobile application development company offering integrated, dynamic and streamlined e-commerce solutions to businesses worldwide. Webtrackker takes pride in developing outstanding mobile applications using Android and iOS based platforms. We have created variety of core Android, core iOS and hybrid applications.

    Mobile Apps Development Company in Delhi NCR

    ReplyDelete

  3. the blog is very interesting and will be much useful for us. thank you for sharing the blog with us. please keep on updating.
    Informatica Training in Chennai

    ReplyDelete
  4. PHP Training in Noida - We provides better PHP course covering the entire course content from basics to the advanced level. It’s better to choose the classroom PHP training that provides the PHP course in noida to get the practical knowledge of working experience.

    ReplyDelete
  5. Java Training in Noida - Croma campus by excellent experienced IT professionals who has more then 10+ Years of real time experience Our trainers has good training actuality so that best quality output will be delivered.s

    ReplyDelete
  6. Croma campus is creative constrictive lecture for SAS training in Noida and implements your knowledge through programming training. Croma campus provide best lecture on practical lab. We have a best mentor. croma campus have provided of online work.

    ReplyDelete
  7. Croma Campus is very famoused for AWS Training in Noida. Croma is best training center in Noida.Croma Provides 100% placement gurantee.and best lab facility and expirience based mentor , giving best fully practally project based training.Croma is giving AWS Training facility according other institute.

    ReplyDelete
  8. Croma campus is providing “Selenium Training in Noida” and best training in noida. Selenium is very important for every programmer and these programs have must for feature growth. croma campus is give placement 100% guarantee. Croma campus is creative constrictive lecture for Selenium Training.

    ReplyDelete
  9. Croma campus is very famous in noida or delhi-ncr. We provided developing or programming training. We have best training mentor. croma campus is give to AngularJS training in noida .we have complete facility and placement guarantee. croma have done already many students place in best .croma is better according other institute.

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete

  11. Croma campus is the finest top rank training center is provided MongoDB training in Noida. croma campus is give placement 100% guarantee. Croma provides quality fundamental driven job training for the student. Croma campus is best training in noida.

    ReplyDelete

  12. Croma campus is very famous in noida or Delhi-ncr. We provided developing or programming training. We have best training mentor. croma campus is give Ethical Hacking training in noida. We have complete facility and placement guarantee. croma have done already many students place in best company. croma is better according other institute.

    ReplyDelete
  13. Croma Campus is specialized different types of Trainings according to need of the students. Croma campus is providing is best Graphics Designing Training in Noida. We are providing totally practical based & project based training through more experience tutor. We are giving 100% placement with training certificate.

    ReplyDelete
  14. Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article.thank you for sharing such a great blog with us. expecting for your.
    Digital Marketing Company in India
    seo Company in India

    ReplyDelete