Tuesday, 3 July 2018

004 LINQ to Object

LINQ offer query against object . You can query an object from in memory collection.Array, list, collections can be complex , and can hold data of a particular type of object. You can query on collection objects with LINQ.


              Before the introduction of LINQ , developer used  for loop to retrieve data from collections and write condition to filter the data. That was very hectic . LINQ introduced simple technique to retrieve the data buy simple writing declarative code that reduce effort and  clean, structure code with more accuracy and  faster than for loop.

Below is the example of LINQ to Object .The first example is of a simple query on a array.The second example is of a collection of a particular type of object.

Example



using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data;
using System.Linq;
using System.Collections;


        namespace ConsoleApplication1
        {
                 class Program
                {
                      static void Main(string[] args)
                            {

                              string[] country = { "India", "Brazil", "China", "USA", "Pakistan", "South Afrika", "Japan", "Russia", "Poland", "Sweden" };


                                 /* Example 1 (Simple Select Statement) */

                                  var AllCountry = from c in country select c;

                                   Console.WriteLine("List of Country");

                                    Console.WriteLine("----------------");



                                   foreach (var c in AllCountry)
                                   {

                                            Console.WriteLine(c);

                                    }

                                    Console.ReadLine();



                                   /* Example 3 (LINQ on Array List) */

                                   ArrayList myList = new ArrayList();

                                   myList.Add(

                                    new student

                                   {

                                         name = "John",

                                         roll = 12,

                                          age = 9

                                   });

                                    myList.Add(

                                     new student

                                     {

                                        name = "John1",

                                        roll = 14,

                                         age = 8

                                       });

                                    myList.Add(

                                    new student

                                   {

                                          name = "John2",

                                          roll = 13,

                                          age = 7

                                      });

                                    myList.Add(

                                     new student

                                    {

                                         name = "John3",

                                         roll = 15,

                                          age = 8

                                    });


                             var query = from student s in myList

                              where s.age > 7

                                      select s;


                          Console.WriteLine("List of Student Over 7 Years of Age");

                          Console.WriteLine("--------------------------------------");



                          foreach (student c in query)
                         {

                                  Console.WriteLine("Name : " + c.name);

                                  Console.WriteLine("Roll : " + c.roll);

                                   Console.WriteLine("Age : " + c.age);

                                   Console.WriteLine("\n");

                           }
                            Console.ReadLine();

                  }

                 }

}

public class student
{
                    public string name { get; set; }
                    public int roll { get; set; }
                    public int age { get; set; }
}

Output :

List of Country
----------------
India
Brazil
China
USA
Pakistan
South Afrika
Japan
Russia
Poland
Sweden

List of Student Over 7 Years of Age
--------------------------------------
Name : John
Roll : 12
Age : 9


Name : John1
Roll : 14
Age : 8


Name : John3
Roll : 15
Age : 8



No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...