i

ASP.Net A Complete Guide

Query Expression

Language Integrated Query[LINQ] :

Uniform query syntax which helps to retrieve data from different data sources. The vital feature of LINQ is it provides the ability to the C# & VB.NET to generate queries to fetch data from the data source. In C#, the LINQ is present in “System. Linq”namespace.

Below are the interfaces provided by the LINQ:

IEnumerable: Exposes the enumerator, which supports a simple iteration over a collection of a specified type.

IQueryable: Provides functionality to evaluate queries against a specific data source wherein the type of data is known.

The most visible "language-integrated" part of LINQ is the query expression. Query expressions are written in declarative query syntax. By using query syntax, you can perform filtering, ordering, and grouping operations on data sources with a minimum of code.

Below are the few features of LINQ:

  • LINQ help us to reduce the code.

  • It provides type checking at compile time.

  • It provides IntelliSense for generic collections.

LINQ has below ways to write:

Query Expression:

The Query Syntax is the same as SQL syntax. It starts with from clause and can be ended with the Selector GroupBy clause.

Example:

IList fruitList = new List() {

    "Apple fruit",

    "Banana fruit",

    "Orange friut" ,

 "Sweet Potato"  

};

var result = from str in fruitList

            where str.Contains("Orange")

            select str;