What is LINQ technology?

Asked

Viewed 1,617 times

15

Today reading some contest questions, appeared one in which it was LINQ technology. Without delving too much into the research I noticed a number of quotes as:

  • LINQ to SQL
  • LINQ to XML (C#)
  • LINQ to Dataset
  • LINQ to Objects (C#)

I don’t quite understand if it’s a mechanism or a pattern or to eat . What is LINQ technology? How is it used in practice?

2 answers

19


Language INtegrated Query or integrated query to language is a technology, and so can be considered a mechanism to facilitate queries to data organized in collections.

LINQ To SQL

The main purpose of its creation was to have a declarative language available for . NET and to some extent replace SQL. So you can write to query for the C# database (may be another language as long as its compiler helps) without being linked to any database. The consultation is transformed into a string SQL for the database being consulted. The query is done through a repository, generally provided by the Entity Framework, but may be any other form that conforms to the LINQ protocols.

So much so that the syntax is very similar to SQL. Some people even criticize the method names because they don’t follow the most functional standard of doing this, but the goal was to be close to SQL.

A database can be accessed by LINQ since it provides a translator from LINQ to your SQL. It’s LINQ Providers (a list of existing).

In fact LINQ is so powerful that it can query databases that do not use SQL, or even that are not relational. For LINQ to work it only needs to have a data collection.

LINQ To XXX

It doesn’t even have to be a database. You can do it in several files, whether it’s plain text, or a specific format, XML, JSON, a language source code, a DataSet of a connection ADO.NET etc., as well as access to external devices (network in various protocols, sockets, etc.). Finally, where there is a stream data can use LINQ. Again, as long as someone provides a set of classes that allow access to this data in an appropriate way.

These simpler cases that do not have a query language, as is the case of SQL, is much easier because it does not have to evaluate every expression and create a text to send to the database or other data access mechanism. Simply have a mechanism that reads the information somewhere and delivers one by one to LINQ, as requested.

LINQ To Objects

Obviously memory collections can be used as well. So a List, Dictionary, array, etc. can be used.

There are cases that it is so simple to provide the data that you do not need to provide something additional to access them. Let’s say you make a new collection to wear, I don’t know, a AVLTree, simply that this collection is enumerable that LINQ already works with it, the standard library provides the necessary mechanisms.

How it works

LINQ is a set of extension methods who manipulate something that is IEnumerable (normal collections) or IQueryable (collections that need to be translated). These methods manipulate item by item in the collection (in several cases through yield) and may be composed of each other.

These methods can be used imperative or declarative (depends on the compiler). In the mandatory form uses such a interface Fluent. In the declarative an evolved form that eliminates "noise" and integrates better with the host language (C#, for example).

To instruct what to do LINQ uses a lot Amble (functioning).

So we can extend the use of LINQ in two ways:

  • we can create new data providers for LINQ to process and
  • we can create new specific query methods to be applied in LINQ, although they cannot be used in declarative form.

See the source of Any() in simple collections (.NET Core). And the source of Any() for external data sources (as far as I know is for SQL Server).

The LINQ imposes a overhead of execution in collections, mainly in memory. So one must think if it is worth its use. Most of the time it is worth, but one has to know how to use.

With the PLINQ creates an abstraction that facilitates the use of parallel scan in collections.

You can see How the logic of Where no Entity Framework works?. The other methods work the same.

More information

You can see examples that Microsoft provides. The links of this answer are full of examples. Real practical examples throughout .

Most languages can have something similar even without the help of the compiler (only the imperative form), just having a general way of treating collections and generators of elements (yield). Just to give you an example made for Typescript.

Read also: What is System.Linq for in C#? (will dup?)

I advise to see the official documentation.

That’s what you can answer here, you can fit more specific questions.

  • You say: "...a certain way to replace SQL...", but in fact it replaces in practice?

  • 1

    @Acklay Yes, in the Entity Framework it replaces. It doesn’t mean that there will never be a situation that it can’t solve. There are cases that SQL is necessary, but in general LINQ is used in the code instead of SQL. LINQ generates SQL for you and sends it to the database.

3

Integrated Language Consultation (LINQ) is an innovation introduced in . NET Framework version 3.5 that bridges the gap between the world of objects and the world of data.

Traditionally, queries in data are expressed as simple strings without type verification in compiling time or Intellisense support. In addition, you will need to learn a different query language for each data source type: SQL databases, XML documents, various web services and so on. LINQ queries a first-class language construct in c#. You write queries regarding collections with rigidity of object types using keywords and familiar operators.

See this link https://msdn.microsoft.com/pt-br/library/mt693042.aspx

Browser other questions tagged

You are not signed in. Login or sign up in order to post.