What is LINQ?

Possible duplicate:
Learning LINQ

Hello to all,

I just want to understand what is LINQ in DOTNET? And how does it work?

Th

+7
linq
source share
5 answers

LINQ is a lot of things, it is a combination of many small things.

This answer will be erratic, sorry. It’s best to wait a bit and see if anyone else can generalize it better and do Google for the keywords that I use.

LINQ stands for " L anguage IN tegrated Q uery", and the most naive interpretation is this: they added an SQL-like syntax to the C # programming language.

So, instead of:

IEnumerable<int> values = otherValues.Where(i => i > 5); 

they have the syntax:

 IEnumerable<int> values = from i in otherValues where i > 5 select i; 

The C # compiler actually translates the second piece of code into the first part of the code, so in fact you are just calling methods in collections.

However, here is another piece of the puzzle. These methods are not actually defined in collections at all. They are defined as extension methods, which means they are defined elsewhere, with some tricks that basically say β€œlet the programmer use these methods as if they were defined in the collection type to begin with, and just fix the code at compile time. "

So, the first part of the code is above:

 IEnumerable<int> values = otherValues.Where(i => i > 5); 

actually ends up compiling like:

 IEnumerable<int> values = Enumerable.Where(otherValues, i => i > 5); 

The Where method is defined here: Enumerable.Where .

The next piece of magic is that the C # compiler doesn't use Enumerable.Where what it does is that it just rewrites the code on the fly to look like the second piece of code in my answer here, and let the normal type output works. In other words, it's going to pretend that you actually wrote a second piece of code, and then see that "otherValues" is a List<T> , where T is int , and then discovers that Enumerable.Where is the one you call.

This means that you can actually create your own Where implementations for types other than collections, and the LINQ syntax will not be wiser.

This means ... things may be requested that are not actually stored in memory. For example, if "otherValues" above is what knows how to get data from the database, another Where method will be called, not the one specified in Enumerable.Where.

This allows other implementations to do their own thing, for example, writing SQL for you, executing it, and packaging the result so that it looks like the calling code, as if it were actually nested, memory for starters.

The next piece of magic is expression. The Where method parameter above, i => i > 5 is in most cases a lambda expression or an anonymous method, and you can actually declare it this way for a collection in memory:

 Func<int, bool> w = delegate(int i) { return i > 5; }; IEnumerable<int> values = otherValues.Where(w); 

However, support for expressions in C # means that you can also declare it as:

 Expression<Func<int, bool>> w = i => i > 5; 

Here, the compiler does not actually store it as a compiled part of the code, but rather a data structure in memory that knows that it takes one argument, compares it with 5 with more than the comparison, and returns the result.Note that you should use lambda- way to write, not as a delegate.

This knowledge allows those other Where implementations, if declared, to accept expressions, not only to hold the "where clause", but to look at it, highlight it, and rewrite it.

This means that the generation of this SQL can be done in the Where method, which knows how to handle SQL code.

Here's the LINQ to SQL declaration of the Where: Queryably.Where method.

So LINQ is a combination of many smaller pieces of technology added to the C # compiler:

+3
source share

These are a few different things.

Linq, as it appears in System.Linq namespace, is a set of extension methods that allows you to query collections directly in code. This is the abbreviation "INtegrated Query Language".

It is also a set of providers that allow you to query different data sources - SQL with Linq to SQL, XML with Linq for XML, etc.

+2
source share

MSDN does a very good job implementing LINQ:

[...]

A .NET-integrated query defines a set of general-purpose standards query operators that allow bypass, filter, and projection to be expressed in a direct declarative way on any .NET-based programming language. Standard query operators allow you to query queries applied to any IEnumerable-based information resource. LINQ allows third parties to expand the set of standards for query operators with new domain-specific operators that are appropriate for the target domain or technology. More importantly, third parties can also replace standard query operators with their own implementations, which provide additional services such as remote evaluation, query translation, optimization, etc. Adhering to the LINQ pattern conventions, such implementations have the same language integration and support tool as standard query operators.

[...]

http://msdn.microsoft.com/library/bb308959.aspx

+2
source share

Think about how to query objects using sql-like syntax. Here is an example copied from http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx

 public void Linq1() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var lowNums = from n in numbers where n < 5 select n; Console.WriteLine("Numbers < 5:"); foreach (var x in lowNums) { Console.WriteLine(x); } } 
0
source share

Linq is a set of extension methods for IEnumerable. This allowed you to ignore some of the details of obtaining objects from collections. When you request your collection through Linq, you do it so that it is declarative, not imperative. This means that your Linq request shows what you want to receive, and not exactly how you will receive it. In the foreach () loop, you will need to clearly indicate how you are going to filter, group, and sort the results. With Linq, these are just a few short instructions, and implementation details are distracted from you.

Many people have a misconception that this is due to SQL due to Linq-to-Sql, but in fact this is only one small part of Linq. You do not need to use L2S to get the full power of Linq, and in fact many people do not. Although Linq-to-SQL in my personal opinion is a cat meow, if you are a .NET store with only SQL Server as your database.

0
source share

All Articles