Monday, April 26, 2021

LINQ Sort Array & List using OrderBy, GroupBy keywords

using Console_OrderBy;
using System;
using System.Collections.Generic;
using System.Linq;

class SortTest
{
    public void SortIntArrayOrderBy()
    {
        int[] numbers = { 7, 5, 9, 8, 1, 6, 2, 4, 3, 9 };
        //SQL syntax
        /*
        IOrderedEnumerable<int> queryNums = from number in numbers
                                    orderby number ascending
                                    select number;
        */
        //method syntax
        IOrderedEnumerable<int> queryNums = numbers.OrderBy(number => number);
        //execute query
        foreach (var item in queryNums)
        {
            Console.WriteLine(item);

        }
    }
    public void SortStrArrayOrderBy()
    {
        string[] cities = { "Goa", "Nagpur", "London", "Paris", "Delhi", "Chennai" };
        //SQL syntax
        /*
        IOrderedEnumerable<string> queryStrs = from city in cities
                                            orderby city
                                            select city;
        */
        //method syntax
        var queryStrs = cities.OrderBy(city => city);
        foreach (var item in queryStrs)
        {
            Console.WriteLine(item);

        }
    }
    public void SortListOfObjectsArrayOrderBy()
    {
        List<Student> students = new List<Student>()
            {
             new Student() { Name = "Ajeet", Age = 27, Gender = "Male", City = "Agra", Scores = 50},
             new Student() { Name = "Mohan", Age = 23, Gender = "Male", City = "Delhi", Scores = 80 },
             new Student() { Name = "Rina", Age = 24, Gender = "Female", City = "Delhi", Scores = 70},
             new Student() { Name = "Mira", Age = 33, Gender = "Female", City = "Agra", Scores = 60 },
             new Student() { Name = "Sita", Age = 28, Gender = "Female", City = "Agra", Scores = 50 },
             new Student() { Name = "Hari", Age = 27, Gender = "Male", City = "Delhi", Scores = 40 },
             new Student() { Name = "Ranjeet", Age = 37, Gender = "Male", City = "Goa", Scores = 50},
             new Student() { Name = "Ravi", Age = 23, Gender = "Male", City = "Delhi", Scores = 30 },
             new Student() { Name = "Roshan", Age = 24, Gender = "Male", City = "Delhi", Scores = 50},
             new Student() { Name = "Bhavna", Age = 33, Gender = "Female", City = "Goa", Scores = 55 },
             new Student() { Name = "Mina", Age = 28, Gender = "Female", City = "Goa", Scores = 45 },
             new Student() { Name = "Ajay", Age = 27, Gender = "Male", City = "Delhi", Scores = 70 },
        };
        //var queryListObjects = from student in students
        //                       orderby student.City
        //                       select student.City;
        //var queryListObjects = from student in students
        //                       group student by student.City
        //                       into groupedCities
        //                       orderby groupedCities.Key
        //                       select groupedCities;
        //Console.WriteLine(System.Environment.NewLine);
        var queryListObjects = students.GroupBy(student => student.City);
        Console.WriteLine("Groups #: "+queryListObjects.Count());
        foreach (var groupc in queryListObjects)
        {
            //each group has a unique key value
            Console.WriteLine(groupc + " ---->> " + groupc.Key + " " + groupc.Count());
            foreach (var s in groupc)
            {
                //console.writeline(s); // student class
                Console.WriteLine(" *** " + s.City);
            }

        }
    }
}
namespace Console_OrderBy
{
    class Program
    {
        static void Main(string[] args)
        {
            SortTest t = new SortTest();
            t.SortIntArrayOrderBy();
            t.SortStrArrayOrderBy();
            t.SortListOfObjectsArrayOrderBy();
            Console.ReadKey();
        }
    }
}

OUTPUT

1
2
3
4
5
6
7
8
9
9
Chennai
Delhi
Goa
London
Nagpur
Paris
Groups #: 3
System.Linq.Grouping`2[System.String,Console_OrderBy.Student] ---->> Agra 3
 *** Agra
 *** Agra
 *** Agra
System.Linq.Grouping`2[System.String,Console_OrderBy.Student] ---->> Delhi 6
 *** Delhi
 *** Delhi
 *** Delhi
 *** Delhi
 *** Delhi
 *** Delhi
System.Linq.Grouping`2[System.String,Console_OrderBy.Student] ---->> Goa 3
 *** Goa
 *** Goa
 *** Goa

No comments:

Post a Comment

Hot Topics