Saturday, April 24, 2021

LINQ SQL clauses Examples- from, where, group, into, and select clauses

using System;
using System.Collections.Generic;
using System.Linq;
/*
 * Data Source
 * Concept of Sequence
 * Query vs Query result
 * Query Expression
 * Query vs Query variable
 * Query variable as Enumerable type
 * IEnumerable type
 * IEnumerable of generic type
 * Difference between Query and 
 * Range variable vs Query variable
 * Method call in Query
 * Single Query with Multiple data sources
 * Query without data transformation of data source returning filtered data
 * Query without data transformation of data source returing count, min, max, average etc. single figure
 * Query with data transformation of data source
 * required clauses from , select or group
 * LINQ query expression must begin with from clause.
 * LINQ query expression must end with either a group clause or a select clause.
 */

namespace Console_LINQ_QueryExpression
{
    class Student
    {
        //auto-implemented properties
        public string Name { get; set; }
        public string Gender { get; set; }
        public string City { get; set; }
        public int Age { get; set; }
        public int[] Scores { get; set; }
    }
    class Test
    {
        public void Linq_Example_01()
        {
            Console.WriteLine("Linq_Example_01: Display data source items");
            /*LINQ with mandatory from and select clauses
             *Data source is initialized directly in LINQ
             */
            var queryvar = from rangevar 
                           in new int[] { 1, 2, 3, 4, 7, 6, 5, 9 }
                           select rangevar;
            foreach (var number in queryvar)
            {
                Console.Write(number + " ");
            }
        }
        public void Linq_Example_02()
        {
            Console.WriteLine("\nLinq_Example_02: where clause to filter source data");
            /*LINQ with where clause */
            var queryvar = from rangevar 
                           in new int[] { 1, 2, 3, 4, 7, 6, 5, 9 }
                           where rangevar > 4
                           select rangevar;
            foreach (var number in queryvar)
            {
                Console.Write(number + " ");
            }
        }
        public void Linq_Example_03()
        {
            Console.WriteLine("\nLinq_Example_03 : orderby clause to sort data source");
            /*LINQ with orderby clause */
            var queryvar = from rangevar
                           in new int[] { 1, 2, 3, 4, 7, 6, 5, 9 }
                           orderby rangevar
                           select rangevar;
            foreach (var number in queryvar)
            {
                Console.Write(number + " ");
            }
        }

        public void Linq_Example_04()
        {
            Console.WriteLine("\nLinq_Example_04 : where and orderby clauses to sort filtered data source");
            /*LINQ with where clause */
            var queryvar = from rangevar
                           in new int[] { 1, 2, 3, 4, 7, 6, 5, 9 }
                           where rangevar > 4
                           orderby rangevar
                           select rangevar;
            foreach (var number in queryvar)
            {
                Console.Write(number + " ");
            }
        }

        public void Linq_Example_05()
        {
            Console.WriteLine("\nLinq_Example_05 : Sorted words and Explicit datatype of range variable and query variable" );
            string[] words = "Think for yourself and let others enjoy the privilege of doing so too.".Split(' ');
            IEnumerable<string> varquery = from string word in words
                       orderby word
                       select word;
            foreach (string wd in varquery)
            {
                Console.Write(wd + " ");
            }
        }

        public void Linq_Example_06()
        {
            Console.WriteLine("\nLinq_Example_06 : group clause with by contextual clause");
            List<Student> students = new List<Student>()
            {
             new Student() { Name = "Ajeet", Age = 27, Gender = "Male", City = "Agra", Scores = new int[]{ 75, 30, 40, 55, 60 } },
             new Student() { Name = "Mohan", Age = 23, Gender = "Male", City = "Delhi", Scores = new int[]{ 55, 60, 20, 30, 40} },
             new Student() { Name = "Rina", Age = 24, Gender = "Female", City = "Delhi", Scores = new int[]{ 40, 30, 45, 55, 77 } },
             new Student() { Name = "Mira", Age = 33, Gender = "Female", City = "Agra", Scores = new int[]{ 80, 43, 40, 55, 69 } },
             new Student() { Name = "Sita", Age = 28, Gender = "Female", City = "Agra", Scores = new int[]{ 70, 20, 40, 59, 32 } },
             new Student() { Name = "Hari", Age = 27, Gender = "Male", City = "Delhi", Scores = new int[]{ 54, 70, 20, 40, 90} },
        };
            var groupedstudents = from student in students
                           group student by student.Gender;
            foreach (var item in groupedstudents)
            {
                //list all the items of Gender key
                Console.WriteLine(item.Key);
            }
            Console.WriteLine("Example of into clause for subquery:--");
            var malestudents = from student in students
                               group student by student.Gender
                               into males
                               where males.Key=="Male"
                               select males;
            foreach (var malestudent in malestudents)
            {
                //Console.WriteLine(malestudent.Key);
                foreach (var m in malestudent)
                {
                
                    Console.WriteLine(m.Name+ ", " + m.City + ", " +
                        m.Gender + ", "+ m.Age);
                }
            }
            Console.WriteLine("List Females:-----");
            var femalestudents = from student in students
                                 where student.Gender == "Female"
                                 group student by student.City
                                 into females
                                 select females;

            foreach (var femalestudent in femalestudents)
            {
                foreach (var f in femalestudent)
                {
                    Console.WriteLine(f.Name + ", " + f.City + ", " 
                        + f.Gender + ", " + f.Age);
                }
            }


        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Test t = new Test();
            t.Linq_Example_01();
            t.Linq_Example_02();
            t.Linq_Example_03();
            t.Linq_Example_04();
            t.Linq_Example_05();
            t.Linq_Example_06();

        }
    }
}

OUTPUT

Linq_Example_01: Display data source items
1 2 3 4 7 6 5 9
Linq_Example_02: where clause to filter source data
7 6 5 9
Linq_Example_03 : orderby clause to sort data source
1 2 3 4 5 6 7 9
Linq_Example_04 : where and orderby clauses to sort filtered data source
5 6 7 9
Linq_Example_05 : Sorted words and Explicit datatype of range variable and query variable
and doing enjoy for let of others privilege so the Think too. yourself
Linq_Example_06 : group clause with by contextual clause
Male
Female
Example of into clause for subquery:--
Ajeet, Agra, Male, 27
Mohan, Delhi, Male, 23
Hari, Delhi, Male, 27
List Females:-----
Rina, Delhi, Female, 24
Mira, Agra, Female, 33
Sita, Agra, Female, 28

No comments:

Post a Comment

Hot Topics