Language Integrated Query
lastmod 2018-08-17created 2017-01-182 edits
LINQ (Language-Integrated Query) is the name for a set of technologies based on the direct integration of query capabilities into the C# language (and also Visual Basic and potentially any other .NET language). With LINQ, a query becomes a high-level language construct, like classes, methods, and events.
For a developer who writes queries, the most visible "language-integrated" part of LINQ is the query expression. Query expressions are written in a declarative query syntax introduced in C# 3.0. With query syntax, you can perform complex filtering, sorting, and grouping operations on data sources with a minimum of code. You use the same basic query expression patterns to query and transform data in SQL databases, ADO.NET datasets, .XML documents and streams, and .NET collections.
Example
The following example shows a complete query operation. The complete operation includes creating a data source, defining the query expression, and executing the query in a foreach statement.
class LINQQueryExpressions
{
static void Main()
{
// Specify the data source.
int[] scores = new int[] { 97, 92, 81, 60 };
// Define the query expression.
IEnumerable<int> scoreQuery =
from score in scores
where score > 80
select score;
// Execute the query.
foreach (int i in scoreQuery)
{
Console.Write(i + " ");
}
}
}
// Output: 97 92 81