An overview of Linq

by Sachin Singh

Posted on Saturday, 23 January 2021

Tags: An overview of Linq

What is Linq

Linq stands for language integrated query. Linq enables us to query a wide range of data source with similar coding style without having the need to know the syntax specific to the data source.

Linq wrt different datasource
Different data sources and respective language to query them

As you can see , in the above diagram to query different data sources we need to know different technologies for example, to query databases we need to know SQL and Ado.Net , to query XML documents we need to know XPATH and XSLT , similarly to query In-Memory objects we must have the knowledge of Arrays and Generics. Having the knowledge of these many technologies is not an easy task , but linq eliminates this issue and now we can query any data source with Linq operators easily in a uniform coding fashion.

Linq was first introduced in .NET Framework 3.5 to query the data from different sources of data such as collections, generics, XML documents, ADO.NET Datasets, SQL, Web Services, etc. in C# and VB.NET using uniform query syntax.

LINQ contains a set of extension methods which allows us to query the different data sources in our application, we can't use any extension method until we import the namespace System.Linq, so please import the required namespace in order to use linq in your application.

we can write LINQ query in any .Net supported programming language like c#,VB etc..

LINQ Architecture

Linq architecture
Linq architecture

Linq architecture is fairly simple . We write linq queries in any .net supported language like c# or Vb.Net to query a data source , there resides a component between Linq queries and the actual data source which converts the linq query into a format that underlying data source can understand , the component is called as Linq provider. There are several Linq providers as you can see in the above diagram for example, Linq to SQL provider converts a linq query to T-SQL that SQL Server database can understand.

Advantages of LINQ

1. We don't need to learn different querying languages for different data sources, LINQ works for all, and we can query all data sources with a similar coding style.
2. In LINQ, we have to write Less code in comparison to the traditional approach, This means LINQ minimizes the code and the same thing is achieved with few lines of code..
3. LINQ provides compile-time error checking as well as intelligence support in Visual Studio, this helps developers like us to avoid run time errors.
4. LINQ provides a lot of built-in methods that we can be used to perform the different operations such as filtering, ordering, grouping, etc. which makes our work easy.
5. The query of LINQ can be reused.

Disadvantages of Linq

Linq has designed for simple queries, as your queries become more complex Linq starts to freak out, in such cases the SQL queries that are generated at a run time becomes significantly bulky and slow, so if you are working with a large database and you need to execute a complex query then it's better to write stored procedures and import that into your domain model (SP in database-first) instead of writing LINQ queries.

Different ways of writing Linq queries

Linq queries are written with the help of Linq Extension methods or Standard query operators, some of them are select, from, where, order by, join, group by, etc. There are two ways or techniques to write Linq queries using the standard query operators.
    1. Using Linq syntax or Sql like query expression as shown below

 var query=  from e in context.Employees
                      where e.Name.StartsWith("s")
                      select e;

    2. Using Extension methods or Lambda expressions as shown below

 var query=  context.Employees.Where(x=>x.Name.StartsWith("s")).ToList();

Which method is better, SQL like query syntax method or lambda expression method?

From a performance perspective there is no difference between the two and which one to use depends on personal preference. Please note that LINQ queries written using SQL-like query expressions are translated into their equivalent lambda expression before they are compiled.

The first approach is better for programmers who are more comfortable with SQL-style queries whereas the second approach is better for programmers who are fine with lambda expressions, delegates, actions, and stuff like that.

But in many places, the extension method technique (Lambda expression) is more powerful than Linq syntaxes (SQL-like query expression). There are queries that you cannot write with SQL like syntax easily or can't write at all, so learn both ways but prefer to use extension methods.