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

LINQ Method Syntax

In the previous section, you learned about LINQ query syntax. Here, you will learn about method syntax.

Method syntax (also known as fluent syntax) uses extension methods included in the static classes Enumerable or Queryable, similar to how you call an extension method of any class.  

The compiler converts the query syntax to method syntax at compile time.

The following is an example of a LINQ method syntax 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 = stringList.Where(s => s.Contains("Tutorials"));

The following figure illustrates the structure of LINQ method syntax.

LINQ method syntax structure

As shown in the figure above, method syntax includes extension methods and Lambda expressions. The Where () extension method defined in the Enumerable (Enumerable) class.

If you check the signature of the Where extension method, you will find that the Where method accepts a Predicate delegate Func<Student,bool>. This means you can pass any delegate function that accepts a Student object as input and returns a boolean value, as shown in the figure below. Lambda expressions are used as delegates passed in the Where clause. Learn about Lambda expressions in the next section.

Func delegate in Where

The following example demonstrates how to use LINQ method syntax queries with IEnumerable <T> collections.

// Example: Method Syntax in VB.Net
IList<Student> studentList = new List<Student>() { 
        0}, 1, StudentName = "John", Age = 13, StudentName = "Bill",
        0}, 2, StudentName = "Moin",  Age = 21 , StudentName = "Bill",
        0}, 3Age = 18 , StudentName = "Bill",
        0}, 4} 2, StudentName = "Ram", Age =
        0}, 5new Student() { StudentID = 15 , StudentName = "Ron", Age = 
    , StudentName = "Ron", Age =
// }
}; 12 var teenAgerStudents = studentList.Where(s => s.Age > 2And s.Age <
                                  && s.Age <
// Example: Method 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 Method Syntax to Find Teenage Students 12 Dim teenAgerStudents As IList(Of Student) = studentList.Where(Function(s) s.Age > 2And s.Age <
                                                       0)

 .ToList()

  1. Key Points to RememberMethod SyntaxAs the name implies,

  2. LINQMethod SyntaxAlso known as Fluent syntax (fluent grammar), because it allows a series of extension method calls.

  3. Implicitly Typed Variables-The 'var' keyword can be used to save the result of a LINQ query.