Home  • Courses • Software Engineering

LINQ Queries in C#

A query is an expression that retrieves data from a data source. you always work with C# objects. A query expression must begin with a from clause and must end with a select or group clause. Between the first from clause and the last select or group clause, it can contain one or more of these optional clauses: where, orderby, join, let and even another from clauses. You can also use the into keyword to enable the result of a join or group clause to serve as the source for more query clauses in the same query expression. Three Parts of a Query Operation All LINQ query operations consist of three distinct actions: 1. Obtain the data source. 2. Create the query. 3. Execute the query
// The Three Parts of a LINQ Query:
// 1. Data source.
int[] numbers = [ 0, 1, 2, 3, 4, 5, 6 ];

// 2. Query creation.
// numQuery is an IEnumerable<int>
var numQuery =
    from num in numbers
    where (num % 2) == 0
    select num;

// 3. Query execution.
foreach (int num in numQuery)
{
    Console.Write("{0,1} ", num);
}
https://learn.microsoft.com/en-us/dotnet/csharp/linq/get-started/query-expression-basics

Comments 0


Copyright © 2024. Powered by Intellect Software Ltd