A query is an expression that retrieves data from a data source. Different data sources have different native query languages, for example SQL for relational databases and XQuery for XML. Developers must learn a new query language for each type of data source or data format that they must support. LINQ simplifies this situation by offering a consistent C# language model for kinds of data sources and formats. In a LINQ query, you always work with C# objects. You use the same basic coding patterns to query and transform data in XML documents, SQL databases, .NET collections, and any other format when a LINQ provider is available.
Three Parts of a Query Operation
All LINQ query operations consist of three distinct actions:
- Obtain the data source.
- Create the query.
- Execute the query.
The following example shows how the three parts of a query operation are expressed in source code. The example uses an integer array as a data source for convenience; however, the same concepts apply to other data sources also. This example is referred to throughout the rest of this article.
private void ThreePartsOfOperation()
{
//First : Data
int[] bunch = [0, 1, 2, 3, 4, 5, 6];
//Second : Write query
var query =
from item in bunch
where (item % 2) == 0
select item;
//Third : Excute qeury
foreach (int item in query)
{
Console.Write("{0,1} ", item);
}
}
The following illustration shows the complete query operation. In LINQ, the execution of the query is distinct from the query itself. In other words, you don't retrieve any data by creating a query variable.
https://learn.microsoft.com/en-us/dotnet/csharp/linq/get-started/introduction-to-linq-queries
'IT개발 > C#' 카테고리의 다른 글
LINQ - Objects (List, Array, Dictionary, Collection) (0) | 2024.10.01 |
---|