How does LINQ work internally?

I like to use LINQ for .net, but I am interested to know how it works internally?

Does anyone know this?

Thks.

+48
linq
Mar 22 '09 at 16:23
source share
5 answers

Asks about a specific aspect of LINQ. This is a bit like asking “How Windows Works” otherwise.

The key parts of LINQ for me, from a C # perspective:

  • Expression trees. This is a representation of code as data. For example, an expression tree may represent the concept of "take a string parameter, call the Length property on it, and return the result." The fact that they exist as data and not as compiled code means that LINQ providers such as LINQ to SQL can parse them and convert to SQL.
  • Lambda expressions. Here are the expressions:

    x => x * 2 (int x, int y) => x * y () => { Console.WriteLine("Block"); Console.WriteLine("Lambda"); } 

    Lambda expressions are converted to either delegates or expression trees.

  • Anonymous types. Here are the expressions:

     new { X=10, Y=20 } 

    They are still statically typed, just the compiler generates an immutable type for you with properties X and Y They are commonly used with var , which allows you to infer the type of a local variable from its initialization expression.

  • Query expressions. Here are the expressions:

     from person in people where person.Age < 18 select person.Name 

    They are translated by the C # compiler into “regular” C # 3.0 (i.e. a form that does not use query expressions). After that, overload resolution, etc. is applied, which is absolutely the key to the possibility of using one query syntax with several data types, without a compiler having any type knowledge, such as Queryable. The above expression will translate to:

     people.Where(person => person.Age < 18) .Select(person => person.Name) 
  • Extension Methods. These are static methods that can be used as if they were instance methods of the first parameter. For example, an extension method like this:

     public static int CountAsciiDigits(this string text) { return text.Count(letter => letter >= '0' && letter <= '9'); } 

    can be used as follows:

     string foo = "123abc456"; int count = foo.CountAsciiDigits(); 

    Note that the implementation of CountAsciiDigits uses a different extension method, Enumerable.Count() .

These are most relevant language aspects. Then there are implementations of standard query operators in LINQ providers such as LINQ to Objects and LINQ to SQL, etc. I have a presentation on how wise it is to simply implement LINQ to Objects - this is on the "Talks" in the C # website in depth.

The way providers such as LINQ to SQL work is usually done through the Queryable class. At their core, they transform expression trees into other query formats, and then create the corresponding objects with the results of these out-of-process queries.

Does it cover everything that interests you? If there is anything you still want to know about, just edit your question and I will go.

+75
Mar 22 '09 at 18:42
source share

In a simple form, the compiler takes your code request and converts it into a bunch of common classes and calls. Below, in the case of Linq2Sql, a dynamic SQL query is created and executed using DbCommand, DbDataReader, etc.

Say what you have:

 var q = from x in dc.mytable select x; 

it is converted to the following code:

 IQueryable<tbl_dir_office> q = dc.mytable.Select<tbl_dir_office, tbl_dir_office>( Expression.Lambda<Func<mytable, mytable>>( exp = Expression.Parameter(typeof(mytable), "x"), new ParameterExpression[] { exp } ) ); 

Lots of generics, huge overhead.

+4
Mar 22 '09 at 17:07
source share

LINQ is basically a combination of C # 3.0 discrete functions:

  • local variable type output
  • Auto properties (not implemented in VB 9.0)
  • extension methods
  • lambda expressions
  • anonymous type initializers
  • understanding of the request

For more information on going there (LINQ), see this Anders video in LANGNET 2008:

http://download.microsoft.com/download/c/e/5/ce5434ca-4f54-42b1-81ea-7f5a72f3b1dd/1-01%20-%20CSharp3%20-%20Anders%20Hejlsberg.wmv

+4
Mar 22 '09 at 17:07
source share

Basically linq is a mixture of some language tools (compiler) and some framework extensions. Therefore, when you write linq queries, they are executed using appropriate interfaces, such as IQuerable. Also note that the runtime has no role in linq.

But it is hard to answer linq judge in a short answer. I recommend you read some book. I'm not sure of the book that talks about the internal components of Linq, but Linq in Action gives good advice about this.

+1
Mar 22 '09 at 16:50
source share

I have a small C # program that demonstrates LINQ implementation in C #.

 class Program { static void Main(string[] args) { //Eventhough we call the method here, it gets called ONLY when the for loop is executed var Cities = LinQFunction(new List<string>() { "Bangalore", "Mysore", "Coorg", "Tumkur", "Kerala", "TamilNadu" }); //LinQFunction() gets callled now foreach(var city in Cities) { Console.WriteLine(city); } } //This function is called ONLY when the foreach loop iterates and gets the item from the collection static IEnumerable<string> LinQFunction(List<string> cities) { foreach (var item in cities) { //Return each 'item' at a time yield return item; } } } 

Use appropriate breakpoints.

0
Mar 13 '14 at 13:26
source share



All Articles