Thursday, May 6, 2021

LINQ Extension Methods Examples Part2




1. Find minimum and maximum age students.
2. Find average scores of students.
3. Find minimum and average age of girl students.
4. Find cities of girl students who are of minimum age.
5. Find all boy students whose name begins with R letter.
6. Find all students whose name contains 'an'.
7. Find all students whose name ends with 'na'.
8. Find all students whose name begins with 'Ra'.
9. Find all boy students whose age is between 15 and 20.
10. Find the total number of boy students whose age is between 15 and 20.
11. Find top 4 scores male students records.
12. Find average scores of male and female students.
13. Find the number of girls living in different cities
14. Find the first female in the list.
15. Find the boy who is 40 years old.

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

namespace ConsoleLINQBasics
{
    class Student
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Gender { get; set; }
        public string City { get; set; }
        public int Scores { get; set; }

        public static List<Student> GetStudents()
        {
            List<Student> _students = new List<Student>()
            {
             new Student() { Name = "Ajeet", Age = 15, 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 = 18, Gender = "Female", City = "Agra", Scores = 60 },
             new Student() { Name = "Sita", Age = 18, Gender = "Female", City = "Agra", Scores = 50 },
             new Student() { Name = "Sohan", Age = 19, Gender = "Male", City = "Delhi", Scores = 40 },
             new Student() { Name = "Ranjeet", Age = 17, 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 = 18, Gender = "Female", City = "Goa", Scores = 55 },
             new Student() { Name = "Divya", Age = 18, Gender = "Female", City = "Goa", Scores = 45 },
             new Student() { Name = "Ajay", Age = 19, Gender = "Male", City = "Delhi", Scores = 70 },
            };
            return _students;
        }
    }
    class Test
    {
        public void Question1()
        {
            Console.WriteLine("-------------------------1");
            //1. Find minimum and maximum age students.
            List<Student> students = Student.GetStudents();
            var q = from s in students
                    where s.Age == (students.Min(x => x.Age))
                    select s;

            foreach (var s in q)
            {
                Console.WriteLine("{0} {1} {2} {3}", s.Name, s.Age, s.Gender, s.City, s.Scores);
            }
        }
        public void Question2()
        {
            Console.WriteLine("-------------------------2");
            //2. Find average scores of students.
            List<Student> students = Student.GetStudents();
            Console.WriteLine(Math.Round(students.Average(x => x.Scores), 2));
        }
        public void Question3()
        {
            Console.WriteLine("-------------------------3");
            //3. Find minimum and average age of girl students.
            List<Student> students = Student.GetStudents();
            Console.WriteLine(students.Where(x => x.Gender == "Female").Min(x => x.Age));
            Console.WriteLine(students.Where(x => x.Gender == "Female").Average(x => x.Age));
        }
        public void Question4()
        {
            Console.WriteLine("-------------------------4");
            //4. Find cities of girl students who are of minimum age.
            List<Student> students = Student.GetStudents();
            var q = from stdn in students
                    where stdn.Gender == "Female" && stdn.Age == 
                    ((from t in students  where t.Gender == "Female" select t into Girls                                                            select Girls.Age).Min())
                    select stdn;

            foreach (var s in q)
            {
                Console.WriteLine("{0} {1} {2} {3}", s.Name, s.Age, s.Gender, s.City, s.Scores);
            }
            //Select Extension method
            Console.WriteLine("**************");
            var q2 = students.Where(t => t.Age == (students.Where(x => x.Gender == "Female").Min(x => x.Age)));

            foreach (var s in q2)
            {
                Console.WriteLine("{0} {1} {2} {3}", s.Name, s.Age, s.Gender, s.City);
            }
        }
        public void Question5()
        {
            Console.WriteLine("-------------------------5");
            //5. Find all boy students whose name begins with R letter.
            List<Student> students = Student.GetStudents();
            var q = students.Where(x => x.Gender == "Male" && x.Name[0] == 'R');
            foreach (var s in q)
            {
                Console.WriteLine("{0} {1} {2} {3}", s.Name, s.Age, s.Gender, s.City, s.Scores);
            }
        }
        public void Question6()
        {
            Console.WriteLine("-------------------------6");
            //6. Find all students whose name contains 'an'.
            List<Student> students = Student.GetStudents();
            var q = students.Where(t => t.Name.Contains("an"));
            foreach (var s in q)
            {
                Console.WriteLine("{0} {1} {2} {3}", s.Name, s.Age, s.Gender, s.City, s.Scores);
            }
        }
        public void Question7()
        {
            Console.WriteLine("-------------------------7");
            //7. Find all students whose name starts with 'Ra'.
            List<Student> students = Student.GetStudents();
            var q = students.Where(t => t.Name.StartsWith("Ra"));
            foreach (var s in q)
            {
                Console.WriteLine("{0} {1} {2} {3}", s.Name, s.Age, s.Gender, s.City, s.Scores);
            }
        }
        public void Question8()
        {
            Console.WriteLine("-------------------------8");
            //8. Find all students whose name ends with 'na'.
            List<Student> students = Student.GetStudents();
            var q = students.Where(t => t.Name.EndsWith("na"));
            foreach (var s in q)
            {
                Console.WriteLine("{0} {1} {2} {3}", s.Name, s.Age, s.Gender, s.City, s.Scores);
            }
        }
        public void Question9()
        {
            Console.WriteLine("-------------------------9");
            //9. Find all boy students whose age is between 15 and 20.
            List<Student> students = Student.GetStudents();
            var q = students.Where(t => t.Age >=15 && t.Age<=20 && t.Gender=="Male").OrderBy(t=>t.Age);
            foreach (var s in q)
            {
                Console.WriteLine("{0} {1} {2} {3}", s.Name, s.Age, s.Gender, s.City, s.Scores);
            }
        }
        public void Question10()
        {
            Console.WriteLine("-------------------------10");
            //10. Find the total number of boy students whose age is between 15 and 20.
            List<Student> students = Student.GetStudents();
            double q = students.Where(t => t.Age >= 15 && t.Age <= 20 && t.Gender == "Male").Sum(t => t.Age);
            Console.WriteLine(Math.Round(q,2));
        }
        public void Question11()
        {
            Console.WriteLine("-------------------------11");
            //11. Find top 4 scores male students records.
            List<Student> students = Student.GetStudents();
            var q = students.OrderByDescending(s => s.Scores).Take(4);
            foreach (var s in q)
            {
                Console.WriteLine("{0} {1} {2} {3}", s.Name, s.Age, s.Gender, s.City, s.Scores);
            }
        }
        public void Question12()
        {
            Console.WriteLine("-------------------------12");
            //12. Find average scores of male and female students.
            List<Student> students = Student.GetStudents();
            var q = students.GroupBy(s => s.Gender).Select(
                grp=> new {
                Avg= Math.Round(grp.Average(x => x.Scores), 2),
                sex = grp.Key });
            foreach (var item in q)
            {
                Console.WriteLine("{0} = {1}",item.sex, item.Avg);

            }
            
        }
        public void Question13()
        {
            Console.WriteLine("-------------------------13");
            //13. Find the number of girls living in different cities
            List<Student> students = Student.GetStudents();
            var grp = students.Where(s => s.Gender == "Female").GroupBy(g => g.City);
            foreach (var g in grp)
            {
                Console.WriteLine("{0} girls are living in {1}", g.Count(), g.Key);
               
            }
        }
        public void Question14()
        {
            Console.WriteLine("-------------------------14");
            //14. Find the first female in the list.
            List<Student> students = Student.GetStudents();
            var q = students.Where(s => s.Gender == "Female").First();
            Console.WriteLine("First girl: {0} {1} {2}", q.Name,q.Age,q.City);
        }
        public void Question15()
        {
            Console.WriteLine("-------------------------15");
            //15. Find the boy who is 40 years old.
            List<Student> students = Student.GetStudents();
            var q = students.Where(s => s.Gender == "Male" && s.Age==40).FirstOrDefault();
            if (q==null)
            {
                Console.WriteLine("No record of boy who is 40 years old.");
            }
            else
            {
                Console.WriteLine("{0} {1} {2} {3}", q.Name, q.Age, q.Gender, q.City, q.Scores);
            }
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            Test t = new Test();
            t.Question1();
            t.Question2();
            t.Question3();
            t.Question4();
            t.Question5();
            t.Question6();
            t.Question7();
            t.Question8();
            t.Question9();
            t.Question10();
            t.Question11();
            t.Question12();
            t.Question13();
            t.Question14();
            t.Question15();
            Console.ReadKey();
        }
    }
}
OUTPUT
-------------------------1
Ajeet 15 Male Agra
-------------------------2
54.17
-------------------------3
18
19.2
-------------------------4
Mira 18 Female Agra
Sita 18 Female Agra
Bhavna 18 Female Goa
Divya 18 Female Goa
**************
Mira 18 Female Agra
Sita 18 Female Agra
Bhavna 18 Female Goa
Divya 18 Female Goa
-------------------------5
Ranjeet 17 Male Goa
Ravi 23 Male Delhi
Roshan 24 Male Delhi
-------------------------6
Mohan 23 Male Delhi
Sohan 19 Male Delhi
Ranjeet 17 Male Goa
Roshan 24 Male Delhi
-------------------------7
Ranjeet 17 Male Goa
Ravi 23 Male Delhi
-------------------------8
Rina 24 Female Delhi
Bhavna 18 Female Goa
-------------------------9
Ajeet 15 Male Agra
Ranjeet 17 Male Goa
Sohan 19 Male Delhi
Ajay 19 Male Delhi
-------------------------10
70
-------------------------11
Mohan 23 Male Delhi
Rina 24 Female Delhi
Ajay 19 Male Delhi
Mira 18 Female Agra
-------------------------12
Male = 52.86
Female = 56
-------------------------13
1 girls are living in Delhi
2 girls are living in Agra
2 girls are living in Goa
-------------------------14
First girl: Rina 24 Delhi
-------------------------15
No record of boy who is 40 years old.

No comments:

Post a Comment

Hot Topics