LINQ

linq
  • LINQ is Language Integrated Query
  • Uniform programming model for any kind of data
  • Just a tool for embedding SQL queries into code.
  • Just another Data abstraction layer.
All the above facts are correct and each focus on a single aspect. LINQ can do a lot more than what is being stated above.

LINQ is a methodology that simplifies and unifies the implementation of any kind of data access. LINQ does not force you to use a specific architecture; it facilitates the implementation of several existing architectures for accessing data. As with every tool, it can be used in both good and bad ways.

What is LINQ?

LINQ is a programming model that introduces queries as a first-class concept into any Microsoft .NET language. However, complete support for LINQ requires some extensions in the language used. These extensions boost productivity, thereby providing a shorter, meaningful, and expressive syntax to manipulate data.

For ex:

var query = from c in Customers
where c.Country == "United Kingdom"
select c.CompanyName;

The above written query is a simple LINQ query that fetches all Customers whose country is United Kingdom.

As you will see, the SQL-like syntax used in LINQ is called a query expression. Languages that implement embedded SQL define only a simplified syntax to put SQL statements into a different language, but these statements are not integrated into the language’s native syntax and type system. For example, you cannot call a function written using the host language in the middle of an SQL statement, although this is possible in LINQ. Moreover, LINQ is not limited to querying databases, as embedded SQL is.

How LINQ Works:

Let see how a LINQ Query works

Customer[] Customers = GetCustomers();

var query = from c in Customers

where c.Country == "United States"

select c;

the compiler generates this code:

Customer[] Customers = GetCustomers();

IEnumerable query =

Customers

.Where( c => c.Country == "United States" );

As you can see, the code apparently calls instance members on the object returned from the previous call. You will see that this apparent behavior is regulated by the extension methods feature of the host language (C# in this case). The implementation of the Where, OrderBy, and Select methods—called by the sample query—depends on the type of Customers and on namespaces specified in previous using statements. Extension methods are a fundamental syntax feature that is used by LINQ to operate with different data domains using the same syntax.

Extension Methods

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.

Relational Model vs. Hierarchical/Graph Model:

At first sight, LINQ might appear to be just another SQL dialect. This similarity has its roots in the way a LINQ query can describe a relationship between entities such as an SQL join:

var query =

from c in Customers

join o in Orders

on c.CustomerID equals o.CustomerID

select new { c.CustomerID, c.CompanyName, o.OrderID };

This is similar to the regular way of querying data in a relational model. However, LINQ is not limited to a single data domain like the relational model is. In a hierarchical model, suppose that each customer has its own set of orders, and each order has its own list of products. In LINQ, we can get the list of products ordered by each customer in this way:

var query =
from c in Customers
from o in c.Orders
select new { c.Name, o.Quantity, o.Product.ProductName };

The previous query contains no joins. The relationship between Customers and Orders is expressed by the second from clause, which uses c.Orders to say “get all Orders of the c Customer.” The relationship between Orders and Products is expressed by the Product member of the Order instance. The result projects the product name for each order row using o.Product.ProductName. Hierarchical relationships are expressed in type definitions through references to other objects.

LINQ Flavors:

LINQ is a technology that covers many data domains. Some of these domains are included in those “LINQ Flavors” that Microsoft provides as part of the .NET 3.5 Framework, as shown in the above picture. Each of these implementations is defined through a set of extension methods that implement the operators needed by LINQ to run over a particular data domain. The access to these features is controlled by the imported namespaces.

LINQ to Objects:

LINQ to Objects has the goal of manipulating collections of objects, which can be related to each other to form a hierarchy or a graph. From a certain point of view, LINQ to Objects is the default implementation used by a LINQ query. LINQ to Objects is enabled including the System.Linq namespace.

LINQ to ADO.NET

LINQ to ADO.NET includes different LINQ implementations that share the need to manipulate relational data. It includes other technologies that are specific to each particular persistence layer:

LINQ to SQL Handles the mapping between custom types in C# and the physical table schema.

LINQ to Entities Is in many ways similar to LINQ to SQL. However, instead of using the physical database as a persistence layer, it uses a conceptual Entity Data Model. The result is an abstraction layer that is independent from the physical data layer.

LINQ to DataSet Makes it possible to query a DataSet using LINQ.

LINQ to XML

LINQ to XML offers a slightly different syntax that operates on XML data, allowing query and data manipulation. A particular type of support for LINQ to XML is offered by Visual Basic 9.0, which includes XML literals in the language. This enhanced support simplifies the code necessary to manipulate XML data. In fact, you can write such a query in Visual Basic 9.0

Summary

We have walked through the LINQ introduction, language enhancements to the LINQ flavors. .NET Language-Integrated Que ry adds query capabilities to the CLR and the languages that target it. The query facility builds on lambda expressions and expression trees to allow predicates, projections, and key extraction expressions to be used as opaque executable code or as transparent in-memory data suitable for downstream processing or translation. The standard query operators defined by the LINQ project work over any IEnumerable-based information source, and are integrated with ADO.NET (LINQ to SQL) and System.Xml (LINQ to XML) to allow relational and XML data to gain the benefits of language-integrated query.

References:

Introducing Microsoft LINQ by Paolo Pialorsi and Marco Russo

Related Posts

    No related posts found
This entry was posted in research, united kingdom. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>