LET us LINQ!!!! (Languange Integrated Query)

With the Introduction in LINQ from framework 3 onwards C# programmers have got sufficient opportunity to filter and  display data by performing Query in C#. 

The basics of Linq starts from the intoduction of a new keyword  "var" .
eg:
      var myname="myblog";
      var data=10;

From the above example its clear that at the time of compilation .net can detect automatically which type is the var variable.  That is what make us easy for coding.

What is var?
The var keyword is used in variable declarations instead of a type. The technical term for var is “implicitly typed local variable declaration.” The variable itself is still statically typed (the type is determined when the code is compiled), and it is still strongly typed (the type cannot change once it has been declared). The difference is that we are no longer explicitly stating the variable’s type in our source code; instead, the compiler infers the type of the variable based upon the initialize for the variable

Now let us See how LINQ is performed on an array to filter data.


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

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

            string[] myarr = new string[5] {"martin","mc","alex","linto","kishore" };

            var filter = from myfilter in myarr where myfilter.StartsWith("m") select myfilter;

            foreach(var data in filter)
            {
                Console.WriteLine("Name:{0}", data);
            }

        }
    }
}

//The Output is...........................

Name:martin
Name:mc



No comments:

Post a Comment