Author : Rahul
Last Modified : 15-Jul-2021
Complexity : Beginner

Explain LINQ in C#


Introduction

LINQ or "Language Integrated Query" was introduced with .NET Framework 3.5 and Visual Studio 2008. By using LINQ, we can query the data from different data sources (collection, dataset, XML document, SQL database).

For example, SQL is a Structured Query Language that is used to get the data from the database. In the same way, LINQ is a structured query syntax that is used to get the data from different data sources (collections, dataset, XML document, SQL database).

Why LINQ

Before .NET Framework 3.5, if a .Net developer wants to add some filter in SQL query, he required knowledge of SQL or there was a dependency of SQL developer. So developers want something so that the dependency of SQL developer is removed and .Net developer not required to learn SQL. This problem is solved after the invocation of LINQ. LINQ use C# syntex to query the data on the collection and it is easy to write. Since LINQ use C# system so the developer does not require to learn SQL to query the data.

Namespaces For LINQ

Using System.Linq
Using System.Data.Linq

 

Advantage of LINQ

  1. LINQ query can be debug through .NET debugger because it is integrated with C#.
  2. LINQ show IntelliSense so that we can write an accurate query.
  3. LINQ syntax highlighted so that we can easily find out syntax error at design time.
  4. LINQ support filtering, ordering and grouping with minimum code.
  5. Writing LINQ query is fast so that development time gets reduced.
  6. With LINQ, it is easy to see the relationship between the two tables.


Disadvantage of LINQ

  1. If the LINQ query is not correct, it will slow down the performance.
  2. LINQ is not good to write complex queries like SQL.
  3. Caching execution plan of the stored procedure is not supported in LINQ.
  4. If we made some changes in the LINQ query, it required to recompile and redeploy to the server.

LINQ provides three ways to write a LINQ query which are as follow:

  1. Query Syntax
  2. Method Syntax
  3. Mixed Syntax


Query Syntax

This is the easiest ways to write a LINQ query. The query syntax is very similar to SQL Query syntax. The result of the query syntax is IEnumerable or IQueryable type.

Syntex

from [identifier to get single record] 
in [collection of data]
let [expression]
where [expression for filteration of data]
order by [expression to sort the collection in ascending or descending order]
group [expression] by [expression for grouping key] 
into [expression for grouping]
select [expression for property selection]

Example
Query to find all elements of an array that is less than 10.

 class Program
 {
   static void Main(string[] args)
   {
     int[] numbers = { 3, 6, 9, 12, 15 };
     
     //Linq Query
     var result_Linq = from p in numbers where p < 10 select p;
     
     //Display Linq Query Result
     Console.WriteLine("Numbers less than 10:");
     foreach(int item in result_Linq)
     {
       Console.WriteLine(item);
     }
     
     Console.Read();
   }
 }

Output

Numbers less than 10:
3
6
9


Method Syntax

This is a little bit complex as compared to query syntax because it uses a lambda expression to write a LINQ query. It is easily understood by .NET CLR so that at compile-time, query syntax is converted into method syntax. The result of the method syntax is IEnumerable or IQueryable type.

Syntex

[collection of data]
.Where [expression for filteration of data]
.OrderBy [expression to sort the collection in ascending or descending order]
.GroupBy [expression for grouping the data]
.Select [expression for property selection]

Example
Query to find all elements of an array that is less than 10.

 class Program
 {
   static void Main(string[] args)
   {
     int[] numbers = { 3, 6, 9, 12, 15 };
     
     //Lambda Expression
     var result_Lambda = numbers.Where(p => p < 10);
     
     //Display Lambda Expression Result
     Console.WriteLine("Numbers less than 10:");
     foreach (int item in result_Lambda)
     {
       Console.WriteLine(item);
     }
     
     Console.Read();
   }
 }

Output

Numbers less than 10:
3
6
9

 

Mixed Syntax

It is the combination of both query syntax and method syntax. For this use query syntax within parenthesis and then use method syntax. The result of the mixed syntax is IEnumerable or IQueryable type.

Syntex

(Query_Syntex).Method_Syntex()

Example

Query to count the number of elements in the array.

class Program
 {
   static void Main(string[] args)
   {
     int[] numbers = { 3, 6, 9, 12, 15 };
     
     //Linq Query
     var result_Linq = (from p in numbers select p).Count();
     
     //Display Linq Query Result
     Console.WriteLine($"Number of elements in the array: {result_Linq}");
     
     Console.Read();
   }
 }

Output

Number of elements in the array: 5

 

There are different operators are available in LINQ. The details of each operator is given in the below link.