Open In App

LINQ | Method Syntax

In LINQ, Method Syntax is used to call the extension methods of the Enumerable or Queryable static classes. It is also known as Method Extension Syntax or Fluent. However, the compiler always converts the query syntax in method syntax at compile time. It can invoke the standard query operator like Select, Where, GroupBy, Join, Max, etc. You are allowed to call them directly without using query syntax.

Creating first LINQ Query using Method Syntax in C#

Example:




// Create the first Query in C# using Method Syntax
using System;
using System.Linq;
using System.Collections.Generic;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // Data source
        List<string> my_list = new List<string>() {
                "This is my Dog",
                "Name of my Dog is Robin",
                "This is my Cat",
                "Name of the cat is Mewmew"
        };
  
        // Creating LINQ Query
        // Using Method syntax
        var res = my_list.Where(a => a.Contains("Dog"));
  
        // Executing LINQ Query
        foreach(var q in res)
        {
            Console.WriteLine(q);
        }
    }
}

Output:
This is my Dog
Name of my Dog is Robin
Article Tags :
C#