Learning LINQ Made Easy: Tutorial 1

In this article, you will learn about LINQ. Language Integrated Query (LINQ) provides a way to query any type of data source. LINQ simplifies this situation by offering a consistent model for working with data across various kinds of data sources and formats.

Introduction

This article is the first article in a Learning LINQ tutorial series. I am writing a complete LINQ tutorial for beginners explaining LINQ in detail. This article is the first article in this series.

Background

Language Integrated Query (LINQ) provides a way to query any type of data source.

MSDN says:

A query is an expression that retrieves data from a data source. Queries are usually expressed in a specialized query language.

The following is the pictorial representation of the scenario for the need for LINQ.

In a .Net application there is a scenario where it needs data from multiple data sources like SQL databases, XML documents, in memory objects like collections or any other type of data source.

dot net application

To retrieve data from multiple data sources the underlying query and technology is like:

  • SQL and ADO.Net for relational databases
  • XQuery and XSLT for XML

LINQ simplifies this situation by offering a consistent model for working with data across various kinds of data sources and formats.

In a LINQ query, you are always working with objects. You use the same basic coding patterns to query and transform data in XML documents, SQL databases, ADO.NET Datasets, .NET collections and any other format for which a LINQ provider is available.

Basically LINQ simplifies the working model with its generic architecture to support many kinds of data sources and provide a common platform to execute the query and get the results.

LINQ Architecture

LINQ Architecture

The preceding is the architecture of LINQ in simple way.

A .Net application uses LINQ queries to communicate with various kinds of data sources like SQL Server, XML documents and in-memory objects. Between SQL queries and data sources one layer of LINQ providers are present that converts the LINQ queries into the format that the underlying data source can understand.

For example if a LINQ query is written to fetch data from SQL Server then the provider LINQ to SQL will convert the underlying query into a Transact SQL that can be understood by the SQL Server database.

Sample Code

  1. // Linq TO SQL  
  2.   
  3. var employees = from listempoyees in context.tblEmployees  
  4. select listempoyees;  
  5. GridView1.DataSource = employees;  
  6. GridView1.DataBind();  
  7.   
  8. // Linq to In memory Object   
  9. // string Array as sample  
  10.   
  11. string[] LstCity = {"Mumbai""Delhi" , "Patna","London","Banglore","Newyork" };  
  12.   
  13. var cities = from cityList in LstCity  
  14. select cityList;  
  15.   
  16. GridView1.DataSource = cities;  
  17. GridView1.DataBind();  
MSDN says that the Query expression consists of the following three clauses:
  • FROM
  • WHERE
  • SELECT

Query expression

The query variable itself takes no action and returns no data. It just stores the information that is required to produce the results for when the query is executed.

All LINQ query operations consist of the following three distinct actions:

  • Obtain the data source.
    1. string[] LstCity = {"Mumbai""Delhi" , "Patna","London","Banglore","Newyork" };  
  • Create the query.
    1. var cities = from cityList in LstCity  
    2. select cityList;  
  • Execute the query.
    1. // 3. Query execution.   
    2. foreach (int c in cities)  
    3. {  
    4.    Console.Write("{0,1} ", c);  
    5. }  

Advantages of LINQ

  • LINQ can be used for querying multiple data sources such as relational data and XML data.

  • LINQ has syntax highlighting and IntelliSense that help to identify compile-time error checking.

  • LINQ is extensible so new types of data sources can be made querable.

  • LINQ is composable in nature and it can be used to solve complex problems into a series of short, comprehensible queries that are easy to debug.

  • LINQ is declarative, it is very easy to understand and maintain.

Disadvantages of LINQ

  • LINQ architecture has another layer for LINQ providers that will provide performance overhead sometimes with complex queries.

  • LINQ is not a precompiled statement like Stored Procedures.

  • Frequent changes must be recompiled and have a deployment overhead for the entire code.

  • No good way to view permissions.

  • When database queries are converted from SQL to the application side, joins are very slow that are very specific to LINQ to SQL.

Conclusion

There are many advantages and disadvantages of using LINQ technology. It is very important to choose very intelligently the LINQ for your application so that the beautiful features of LINQ can be utilized and the application can be more efficient. It should not be a performance overhead and problem for your application.

More tutorials on LINQ will be coming shortly.

Keep smiling and keep learning.

References: Introduction to LINQ Queries (C#).