Live from TechDays 2006 – Episode Eight

Written by Yves

April 6, 2006

Session D308 – LINQ



Probably the most advanced session of the day…


The problem we face every day is to transform relational data into hierarchical objects and that is why O/R mapper exists such as Nhibernate for instance. The representation of these two models is totally different. But, retrieving objects with a query like we do in traditional databases would be really nice. One of the answers could be LINK, a language agnostic concept to query not only data, but objects as well. More precisely, to query collections of objects. LINQ  means Language Integrated Query. Take for example the following statement :


var c = from Customers


            where c.City == ‘London’


            select c.Name


Var is then a new data type which is completely different from the object type. It is a location to store a projection which is, in fact, the result of a query. If you open an assembly containing such code with ILDASM, you will see that c is translated to a projection class. From this c variable, it is possible to cycle through it to display its properties. The most efficient way to do it is to avoid the use of the foreach loop statement. Indeed, this instruction is transformed to a bunch of code included in a try/catch block which is really a performance killer. That means if you want to optimize your code for speed, look at your loop statements and replace your foreach loops by a standard for(int i = 0; i < c.Length; i++). The speed will increase by a factor of 10.


You May Also Like…

0 Comments

Submit a Comment

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