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


19 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. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. This comment has been removed by the author.

    ReplyDelete

  10. 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

  11. 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
  12. 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
  13. You made some decent factors there. I looked on the internet for the difficulty and found most individuals will associate with along with your website.Keep update more excellent posts.
    Office Interiors in Chennai
    Home Interior Decorators in Chennai

    ReplyDelete
  14. Thank you for sharing such a nice and interesting blog with us. I have seen that all will say the same thing repeatedly. But in your blog, I had a chance to get some useful and unique information.

    Data science training
    Bigdata Hadoop training
    Data Analytics Training
    Devops training
    Selenium training

    ReplyDelete
  15. I am completed training last week. It was a very good 3 month session which included excellent training content, live examples, mock exams etc. Thanks to the trainer and Simplilearn.
    Machine Learning Training in Gurgaon
    Devops Training in Gurgaon
    Python Training in Gurgaon

    ReplyDelete
  16. Dzone is a training institute and we have provided different types of projects to all students who can easily express their ideas and projects. We are the motive is we provide the best solution as per your needs and demands and then you can easily understand the whole concepts. This training just is affordable to all the students and then they can easily purchase and learn this concept.

    https://bit.ly/downloadITprojects


    other links provide:-

    https://bit.ly/reactjsDzone
    https://traininginstituteinjaipur.net/

    ReplyDelete
  17. Thanks for sharing such captivating information with us. And I hope you will share some more information about. PHP Course in Noida please keep sharing!

    ReplyDelete