LINQ Tutorial: Part 1

In this article you will learn the basics of LINQ as Part 1 of the series.

Look at given points

  • LINQ stands for Language-Integrated Query.
  • LINQ was introduced in .NET Framework 3.0.
  • LINQ enables the query of data from any type of data source, including databases, XML, List, Array and so on.

Data source using LINQ
Figure 1:
Data source using LINQ

As the preceding image shows we can query data from any kind of data source using LINQ.

Why LINQ

As we have seen in the figure, LINQ is a technology that is used to query data from any kind of data source. The main advantages of using LINQ is if a programmer is acquainted with a LINQ query he can query data from any kind of database, else for each datasource he must learn other languages or approaches.

For example:

  1. For SQL Server he must learn SQL queries.
  2. For XML Xpath\Xquery.
  3. For Array or Collection we must learn various approaches to retrieve data.

Thus to overcome these drawbacks, Microsoft provides a technology called LINQ that queries data from all data sources.
The second and most important use of LINQ queries are they are totally object oriented languages\queries.
Let me explain these things.

When we are working with a real time project then we are dealing with the following 2 types of languages.

  • Object Oriented Languages
  • Relational Languages

Language
Figure 2:
Language

The front-end is the combination of these 2 types of languages.

Object Oriented Relational
Figure 3: Object Oriented Relation

For the combination of both of the languages the following disadvantages are listed.

  • No intelligence does not support for relational language.
  • No compile time syntax checking is there.

Thus LINQ overcomes these demerits and provides solutions for both the problems.

LINQ
Figure 4: LINQ

The syntax of a LINQ query is:
Syntax of LINQ
Figure 5: Syntax of LINQ

  1. A LINQ query starts with the from statement and ends with a select statement.

  2. When writing queries in LINQ to Collections whereever we need a column name in all those places we need to use an alias name of the collection because we don't have any specific column here.

  3. Clauses are optional attributes that can be used on a query for searching and sorting the data.

  4. To write queries in LINQ we need to import the namespace System.Linq.

  5. Like SQL or Oracle in LINQ, we also have:

    • P Predefined functions
    • C Clutches

LINQ Architecture
LINQ Archicture
Figure 6: LINQ Architecture

  1. Assume we have our application in .NET and the back end is SQL Server. We need to retrieve data from a table and show it in the front-end.

  2. So first of all we will write a LINQ query, the LINQ query is converted into transact SQL using the the provider LINQ to SQL.

  3. Database doesn't understand these LINQ queries so we need to convert to transact SQL using the LINQ to SQL.

  4. Thus this query executes and provides the data back to the front-end.

LINQ Provider

LINQ provider is a component between the LINQ query and actual data source that converts the LINQ query into a format that the data source can understand.

LINQ Provider
Figure 7:
LINQ Provider