latest Post

LINQ Query Syntax

LINQ query syntax:

from <range variable> in <IEnumerable<T> or IQueryable<T> Collection>

<Standard Query Operators> <lambda expression>

<select or groupBy operator> <result formation>
Example:
// string collection
IList<string> stringList = new List<string>() { 
    "C#",
    "VB.NET",
    "C++",
    "MVC" ,
    "Java" 
};

// LINQ Query Syntax
var result = from s in stringList
            where s.Contains("Java") 
            select s;
Query syntax starts with a From clause followed by a Range variable. The From clause is structured like "From rangeVariableName in IEnumerablecollection". In English, this means, from each object in the collection. It is similar to a foreach loop: foreach(Student s in studentList).
After the From clause, you can use different Standard Query Operators to filter, group, join elements of the collection. There are around 50 Standard Query Operators available in LINQ. In the above figure, we have used "where" operator (aka clause) followed by a condition. This condition is generally expressed using lambda expression.
LINQ query syntax always ends with a Select or Group clause. The Select clause is used to shape the data. You can select the whole object as it is or only some properties of it. In the above example, we selected the each resulted string elements.

About Mallikarjun A

Mallikarjun A
Recommended Posts × +

0 comments:

Post a Comment