LINQ Query Operations

Learn the basics of LINQ query operations.

Overview

Every LINQ query operation consists of three distinct actions:

  • Defining a data source
  • Defining the query
  • Executing the query

We’ll review these actions using the code sample below:

{
    "version": "0.2.0",
    "configurations": [
        {
            // Use IntelliSense to find out which attributes exist for C# debugging
            // Use hover for the description of the existing attributes
            // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
            "name": ".NET Core Launch (console)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            // If you have changed target frameworks, make sure to update the program path.
            "program": "${workspaceFolder}/bin/Debug/net6.0/QueryData.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
            "console": "internalConsole",
            "stopAtEntry": false
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach"
        }
    ]
}
Using a LINQ query to output odd numbers

Click the “Run” button of the code widget above, then execute the command below in the terminal:

Press + to interact
dotnet run

Defining a data source

In a LINQ query operation, the first step is to define the data source. A LINQ data source is any object that supports the generic IEnumerable<T> ...

Get hands-on with 1400+ tech skills courses.