What is Query in LINQ?
A query is an expression which is used to recover data from the data source. Generally, queries are expressed in some specialized language. Different types of languages are developed to access the different type of data sources like SQL for a relational database, XQuery for XML, etc. So, every time the developer needs to learn different type of languages for a different type of data sources, this situation leads to the developer to develop a language through which they can access any type of data source with the help of a single language.
So this requirement is fulfilled by the LINQ query, using LINQ query you can access any type of data source like XML document, SQL database, ADO.NET dataset, etc. provided by the LINQ provider. In LINQ query, the query always returns the result as an object, which allows you to use the object-oriented approach on the result and not to worry about transforming different data format into an object.
Example:
// C# program to demonstarte the // Simple query example using System; using System.Linq; class GFG { // Main Method static public void Main() { // Creating data source string[] language = {"C#", "VB", "Java", "C++", "C", "Perl", "Ruby", "Python"}; // Creating a query to get the // value from the data source var result = from lang in language where lang.Contains('C') select lang; // display the result of the query foreach(var l in result) { Console.WriteLine(l); } } } |
Output:
C# C++ C
In the above example, the LINQ query contains three different actions:
- Get data source: In the above example, the data source is an array.
string[]language = {"C#", "VB", "Java", "C++", "C", "Perl", "Ruby", "Python"};Which implicitly support generic IEnumerable<T> interface. In LINQ query, it is a basic rule that the data source of any object must support IEnumerable<T> interface or interface that inherits from the IEnumerable<T> interface.
- Create Query: Now the next step is to create a query. With the help of the query, you can get the information from the data source. The query is stored in the query variable and which is initialized with the query expression. The query expression contains the operation that you want to perform on the data source, generally, query expression contains three clauses, i.e, from, where, and select. The from clause is used to specify the data source, where clause applies the filter and select clause provides the type of the returned items.
Example:
var result = from lang in language where lang.Contains('C') select lang;Here result in the query variable which is initialized with a query expression.
Note: The query variable itself does not perform any operation. It only used to store the result of the query expression and used when the query executes.
- Execute Query: The query variable stores the result of the query expression. But the execution of the query takes place when you iterate over the query variable using foreach loop to display the result of the query. When you use the foreach loop to execute the query, then it is known as deferred execution. In the above example, we use deferred execution.
foreach(var l in result){ Console.WriteLine(l); }
Recommended Posts:
- LINQ | Query Syntax
- LINQ (Language Integrated Query)
- LINQ | Set Operator | Except
- LINQ | Let Keyword
- LINQ | Element Operator | Last
- LINQ | Quantifier Operator | All
- LINQ | Filtering Operator | where
- Aggregation Function in LINQ
- LINQ | Quantifier Operator | Any
- LINQ | Quantifier Operator | Contains
- LINQ | Element Operator | First
- LINQ | How to find the sum of the given Sequence?
- LINQ | Set Operator | Union
- LINQ | Set Operator | Distinct
- LINQ | Partition Operator | Take
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


