Open In App

LINQ Query Syntax

Last Updated : 12 Sep, 2025
Comments
Improve
Suggest changes
9 Likes
Like
Report

LINQ (Language Integrated Query) provides two ways to write queries in C#

  1. Query Syntax (Query Expression Syntax)
  2. Method Syntax (Fluent Syntax)

Query Syntax

LINQ Query Syntax, also called Query Expression Syntax, is a SQL-like way to write queries in C# or VB.NET directly in code.

It uses keywords such as from, where, select and group to query data from collections, arrays, XML, databases or other IEnumerable/IQueryable sources, providing readable and type-safe queries without needing separate query languages.

Creating LINQ Query using Query Syntax

Step 1: First add System.Linq namespace in your code.

using System.Linq;

Step 2: Next, create data source on which you want to perform operations.

For example:

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"

};

Step 3: Now create the query using the query keywords like select, from, etc.

For example:

var res = from l in my_list
where l.Contains("my")
select l;

  • Here res is the query variable which stores the result of the query expression.
  • The from clause is used to specify the data source, i.e, my_list, where clause applies the filter, i.e, l.Contains("my") and select clause provides the type of the returned items.
  • l is the range variable.

Step 4: Last step is to execute the query by using a foreach loop.

For example:

foreach(var q in res){
Console.WriteLine(q);
}

Example: Program to demonstrate LINQ Query Syntax

CSharp
using System;
using System.Linq;
using System.Collections.Generic;

class GFG {

    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
        var res = from l in my_list where l.Contains("my") select l;

        // Executing LINQ Query
        foreach(var q in res) { Console.WriteLine(q); }
    }
}

Output
This is my Dog
Name of my Dog is Robin
This is my Cat
Suggested Quiz
3 Questions

A LINQ query written in query syntax begins with ______ and ends with ______.

  • A

    select, from

  • B

    from, select or group

  • C

    where, select

  • D

    query, end

Explanation:

Query syntax always starts with from and ends with either select or group clauses.

In LINQ Query Syntax, what is the purpose of the from keyword?

  • A

    Filters the data

  • B

    Specifies the data source

  • C

    Projects the data

  • D

    Executes the query

Explanation:


Given the following LINQ Query

C#
var res = from l in my_list
          where l.Contains("my")
          select l;

What does the where clause do?


  • A

    Selects the output type

  • B

    Specifies the data source

  • C

    Filters elements based on a condition

  • D

    Iterates through the query

Explanation:


Quiz Completed Successfully
Your Score :   2/3
Accuracy :  0%
Login to View Explanation
1/3 1/3 < Previous Next >

Article Tags :

Explore