Open In App

What is Query in LINQ?

Improve
Improve
Like Article
Like
Save
Share
Report

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 formats into an object. 
 

Example:
 

C#




// C# program to demonstrate 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:
 

  1. 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.

  2. 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.

  3. 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);
        }


Last Updated : 06 Aug, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads