Sunday 14 July 2013

LINQ - Select the required data from collection





























                                                                                                                                                                     
As the Dot Net have more techniques to iterate and fetch the data from collection, One of the best thing is LINQ. Linq is used in Objects, XML, Dataset etc.

Now, we are going to see a Linq sample Which will select the records from Collection of custom template.

In this Example we will create a Employee class with some property [Name,Age,Sex,Country, Phone Number],Based on that class create a employee collection .

    enum Gender
    {
        Male,
        Female
    }

    class Employee
    {
        public string Name { set; get; }

        public string Country { set; get; }

        public Gender Sex { set; get; }

        public int Age { set; get; }

        public int PhoneNo { set; get; }

    }


Select the employees who are male
      




















class Program
    {
        static List GetEmployeeCollection()
        {
            List _emp = new List();
            _emp.Add(new Employee() 
                    { 
                        Name = "Stepen", 
                        Age = 31, 
                        Sex=Gender.Male, 
                        Country = "US", 
                        PhoneNo = 2341094 
                    });

            _emp.Add(new Employee() 
                    { 
                        Name = "Daniel", 
                        Age = 21,
                        Sex=Gender.Male, 
                        Country = "US",
                        PhoneNo  = 4563234 
                    });

            _emp.Add(new Employee() 
                    { 
                        Name = "Suresh", 
                        Age = 22,
                        Sex = Gender.Male, 
                        Country = "INDIA", 
                        PhoneNo = 8574094 
                    });
            _emp.Add(new Employee() 
                    { 
                        Name = "Indhu", 
                        Age = 35, 
                        Sex = Gender.Female, 
                        Country = "INDIA", 
                        PhoneNo = 23417546 
                    });
            _emp.Add(new Employee() 
                    { 
                        Name = "Vinay", 
                        Age = 41, 
                        Sex = Gender.Male, 
                        Country = "UK", 
                        PhoneNo = 23456788 
                    });
            _emp.Add(new Employee() 
                    { 
                        Name = "Sundar", 
                        Age = 21, 
                        Sex = Gender.Male, 
                        Country = "UK", 
                        PhoneNo = 33425673 
                    });
            _emp.Add(new Employee() 
                    { 
                        Name = "Mark", 
                        Age = 36, 
                        Sex = Gender.Male, 
                        Country = "INDIA", 
                        PhoneNo = 88765456 
                    });
            _emp.Add(new Employee() 
                    { 
                        Name = "Alex", 
                        Age = 26, 
                        Sex = Gender.Male, 
                        Country = "US", 
                        PhoneNo = 88764546 
                    });
            _emp.Add(new Employee() 
                    { 
                        Name = "Maria", 
                        Age = 27, 
                        Sex = Gender.Male,
                        Country = "JAPAN", 
                        PhoneNo = 33245433 
                    });
            return _emp;
        }

        static void Main(string[] args)
        {   
            /* print  the employees information who are male  */
            foreach (Employee emp in GetEmployeeCollection().
                                                         Where(x => x.Sex == Gender.Male))
            {
                Console.WriteLine(" Employee Name {0},\tAge {1},\tSex {2},\tcountry {3}", emp.Name, 
                                                          emp.Age, emp.Sex, emp.Country);
            }

            Console.Read();
        }

    }

Output















 Select the employees who are born in "US"

/* print  the employees information who are in Country "US"  */
            foreach (Employee emp in GetEmployeeCollection().Where(x => x.Country == "US"))
            {
                Console.WriteLine(" Employee Name {0},\tAge {1},\tSex {2},\tcountry {3}", 
                                                emp .Name, emp .Age, emp .Sex, emp .Country);

            }

Output :





Select All Emplloyees who are the age above 26 in India

 /* print  the employees who are in Country "India" and age is greater than 26   */
            foreach (Employee emp in GetEmployeeCollection().Where(x => x.Country == "INDIA"  &&                                                                                                                 x.Age > 26 ))
            {
                Console.WriteLine(" Employee Name {0},\tAge {1},\tSex {2},\tcountry {3}", 
                                                emp .Name, emp .Age, emp .Sex, emp .Country);

            }

Output 


Group the employees based on the Country

 /* print  the employees information who are in Country "US"  */
             foreach (var empgrp  in GetEmployeeCollection().GroupBy(x=>x.Country).Select(x=>new                                                                                                                                              {x.Key,x}))
            {
                Console.WriteLine("Country {0}", empgrp.Key);
                foreach(Employee emp in empgrp.x)
                Console.WriteLine(" Employee Name {0},\tAge {1},\tSex {2}", emp.Name, emp.Age,                                                               emp.Sex);
                Console.WriteLine();
            }

            Console.Read();



Output

Country US Employee Name Stepen, Age 31, Sex Male Employee Name Daniel, Age 21, Sex Male Employee Name Alex, Age 26, Sex Male Country INDIA Employee Name Suresh, Age 22, Sex Male Employee Name Indhu, Age 35, Sex Female Employee Name Mark, Age 36, Sex Male Country UK Employee Name Vinay, Age 41, Sex Male Employee Name Sundar, Age 21, Sex Male Country JAPAN Employee Name Maria, Age 27, Sex Male