LINQ Query Operations
Learn the basics of LINQ query operations.
We'll cover the following...
We'll cover the following...
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:
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> interface or an interface that inherits from it, such as IQueryable<T>.
- Line 3 of Program.csin the SPA widget above uses an integer array namednumbersas a data source. This data source implicitly supports the genericIEnumerable<T>interface.
Defining the query
The query defines what information to retrieve from a data source. In addition, it specifies how that information should be filtered, grouped, or sorted before returning it. The query is initialized with a query expression and stored in a query ...