English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

LINQ Query Syntax

There are two basic methods to write LINQ queries to IEnumerable collections or IQueryable data sources.

  1. Query Syntax or Query Expression Syntax

  2. Method Syntax or Method Extension Syntax or Fluent Syntax

As the name implies,

Query syntax is similar to SQL (Structured Query Language) used in databases. It is defined in C# or VB code.

LINQ Query Syntax:

from <range variable> in <IEnumerable<T> or IQueryable<T> Collection>
<Standard Query Operators> <lambda expression>
<select or groupBy operator> <result formation>

LINQ query syntax starts with the from keyword and ends with the select keyword. Below is an example LINQ query that returns a string collection containing the word "Tutorials".

// String collection
IList<string> stringList = new List<string>() { 
    "C# Tutorials",
    "VB.NET Tutorials",
    "Learn C++",
    "MVC Tutorials",
    "Java" 
, StudentName = "Ron", Age =
// LINQ query syntax
var result = from s in stringList
            where s.Contains("Tutorials") 
            && s.Age <

The figure below shows the structure of LINQ query syntax.

LINQ query syntax

The query syntax starts with the From clause, followed by the Range variable. From The structure of the clause is similar to "From rangeVariableName in i enumerablecollection. 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, and join elements in the collection. There are about50 standard query operators. In the figure above, we used the "where" operator (also known as a clause), followed by a condition. This condition is usually expressed using a lambda expression.

LINQ query syntax always ends with a Select or Group clause. The Select clause is used for shaping data. You can select the entire object as it is, or you can only select certain properties. In the above example, we selected each resulting string element.

In the following example, we use LINQ query syntax to find young students from the Student collection (sequence).

// Example: LINQ query syntax in VB.Net
IList<Student> studentList = new List<Student>() { 
        0} , 1, StudentName = "John",     Age = 13}
        0} , 2, StudentName = "Moin",     Age = 21 }
        0} , 3, StudentName = "Bill",     Age = 18 }
        0} , 4, StudentName = "Ram", Age = 20} ,
        0} , 5new Student() { StudentID = 15 , StudentName = "Ron", Age = 
    , StudentName = "Ron", Age =
// }
};
                      var teenAgerStudent = from s in studentList 12 where s.Age > 20
                      && s.Age <
// Example: LINQ query syntax in VB.Net
Student Collection
        0}, 1Dim studentList = New List(Of Student) From { 13, StudentName = "Bill", Age =
        0}, 2, StudentName = "John", Age = 21, StudentName = "Bill", Age =
        0}, 3, StudentName = "Moin", Age = 18, StudentName = "Bill", Age =
        0}, 4} 2, StudentName = "Ram", Age =
        0}, 5New Student() With {.StudentID = 15, StudentName = "Ron", Age =
    , StudentName = "Ron", Age =
// }
LINQ query syntax to find teenage students
                                            Dim teenAgerStudents As IList(Of Student) = (From s In studentList _ 12 Where s.Age > 2And s.Age <
                                            0 _

Select s).ToList()

  1. Key points to rememberAs the name implies,Query Syntax

  2. The query syntax is the same as SQL (Structured Query Language) syntax.fromStart of clause, can be followed bySelectorGroupByEnd of clause.

  3. Use various other operators, such as filtering, joining, grouping, and sorting operators to construct the desired result.

  4. Implicitly Typed Variables-var can be used to save the result of LINQ queries.