LINQ for Beginners - Part 1

Now I'm going to talk about another Important thing you should learn about when you are dealing with C# language. It's Language Integrated Query (LINQ). here is some point you should know before go to the coding segment.


  • LINQ is set of C# 3.0 language features and framework features for writing  structured type-safety queries over local object collections and remote data sources 
  • LINQ enable query any collection Implementing IEnumerable<>, whether an Array, ArrayList, List or XML Dom or Database Table 
  • LINQ lives under System.LINQ and System.LINQ.Expression namespaces.
  • The basic units of data in LINQ are sequences and elements 
  • A sequence is any object that implements the generic IEnumerable interface and an element is each item in the sequence. This is called a local sequence
                        string[] names ={"sam", "John"};
  • A Query operator is a method that transforms a sequence. A typical Query operator accepts an input sequence & emits an transformed output sequence. There are around 40 query operators.

okay now lets go and do simple LINQ Queries. for that go to visual studio and create a console application Project. this first example will print names where length is more than 4

   /// <summary>  
   /// Main Class  
   /// </summary>  
   class Program  
   {  
     /// <summary>  
     /// Main Method of the Application  
     /// </summary>  
     /// <param name="args"></param>  
     static void Main(string[] args)  
     {  
       string[] names = { "Test1", "Test2232", "Test3212" };  
   
       //Simple LINQ Example to print the names where the length is more that 4   
       var filterName = names.Where(n => n.Length > 5);  
       foreach (var name in filterName)  
       {  
         Console.WriteLine(name);  
       }  
       Console.ReadLine();  
     }  
   }  
   



so one advantage of the LINQ Query is it allow to do chain of operations at a given time. from next example I'm going to show how to do chain of operation.

   /// <summary>  
   /// Main Class  
   /// </summary>  
   class Program  
   {  
     /// <summary>  
     /// Main Method of the Application  
     /// </summary>  
     /// <param name="args"></param>  
     static void Main(string[] args)  
     {  
       string[] names = { "John", "Mary", "Sam" };  
         
       var filterName = names.Where(n => n.Contains("M")).OrderBy(n=>n.Length).Select(n=>n.ToUpper());  
       foreach (var name in filterName)  
       {  
         Console.WriteLine(name);  
       }  
       Console.ReadLine();  
     }  
   }  

>

Hmmm...Now I Think you get and Idea What is LINQ and What kind of operations you can do using this LINQ feature. from next Post onward I'm going to dive deep into each and every part in LINQ. See you soon.. Bye bye. If you have any question related to LINQ please put it as a comment I'll help you whenever I can. okay.

Comments

Popular posts from this blog

Deep dive into OpenXML Part 1

Deep dive into OpenXML Part 2

Convert XML data to dynamic (ExpandoObject) Object