Language Integrated Query (LINQ)

    1 Votes


The LINQ or Language Integrated Query is constant query syntax in C# which is used to save and retrieve the data. It is one of the most important feature of .Net Framework. It consists of operators which are used to query data. To query the data it needs to be encapsulated as an object. If the data source is not an object, then firstly it is converted into an object for the LINQ to perform a query.

For example, SQL is used to save and retrieve the data from the database, so in the same way LINQ is a structured query syntax which is already built in C# to save and retrieve the data from different data sources like SQL server database. Some of the LINQ types are:

  • LINQ to Objects – It is in memory collections.
  • LINQ to SQL – For SQL Server databases.
  • LINQ to XML – For XML documents.
  • LINQ to DataSets – data in a ADO.NET DataSet.

LINQ Features

  • Automatic Properties.
  • Object and Collection Initializers.
  • Inferred and Anonymous Types.
  • Extension Methods
  • Lambda Expressions

Automatic Properties

In the automatic properties the property declaration is more compact. When the property is declared the compiler will create an anonymous and private backing field that is not accessible except through the property getter and setter. For example:

public string Name{get; set;}

Object and Collection Initializers

In the object and collection initializers it initializes the objects without calling a constructor for the object explicitly. The initializers are used in query expressions, when they project the source data into a new data type. Let us have an example class Student with public Name and Rollno properties, object initializer can be initialized as:

Student stu = new Student {Name = "ABC", Rollno= "20"};

Anonymous Types

In the anonymous type it is built by the compiler and the type name is only available to the compiler. It provides a convenient way to combine a set of properties temporarily in a query result without defining a separate named type. They are initialized with an object initializer, and new expression like this:

select new {name = stu.Name, roll = stu.Rollno};

Extension Method

The extension method is a static method which is associated with a type, so they are called as an instance method on the type and this also allows us to add some new methods to existing types without modifying them. The standard query operators are the set of extension methods which provides LINQ query functionality for any type which implements IEnumerable<T>.

Lambda Expressions

In the lambda expression they act as an inline function where they uses this “=>” operator to separate input parameters from the function body and it can be converted as an expression tree or at a compile time to a delegate. In LINQ programming, the lambda expressions are encountered when we make direct method calls to the standard query operators.

Advantages

  • It helps to find out errors during design time.
  • It helps in writing more accurate queries easily.
  • Writing code is faster in LINQ and reduces the development time.
  • It allows conversion of one data type to another. 
  • Debugging is easy due to integration in the C# language.

Example:

class IntroToLINQ
{        
    static void Main()
    {     
        int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };
        var numQuery = from num in numbers
            where (num % 2) == 0
            select num;

        foreach (int num in numQuery)
        {
            Console.Write ("{0,1} ", num);
        }
    }
}

Output:

0 2 4 6