2

http://geekswithblogs.net/Martinez/archive/2009/06/29/understanding-expression-trees.aspx

By reading this article that expline what are Expression trees and what they are used for I have some question thats the article those not answer me clearly.

A. What is the difference between Data Structure and 'compiled piece of code'. * I know that data structure is Array,List,.. But this is not the answer I am looking for.

B. Why is it better to use data structure in Entity Framework than 'compiled piece of code' in terms of efficiency.

Stav Alfi
  • 13,139
  • 23
  • 99
  • 171

2 Answers2

3

Answer to both A and B.

The compiled piece of code can not be reverse engineered into something that you can translate to SQL. There are to many details that made the original concepts you want to translate to SQL go away. The expression trees can be analyzed and understood at run time, when the compiled code can not.

Another meta representation of the queries would be possible to use, but the expression trees gives the advantage that existing plumbing in the compiler can be used for a lot of the heavy work.

Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108
  • While true, I am not sure this really answers the question above. For A) Expression trees don't have to be tied to EntityFramework, SQL etc. These are a way of expressing your program as a tree. As such they cannot be executed without compiling them first. For B) The difference is that you can actually build a expressions dynamically based on IQueryable (in case of EF it would be ObjectSet/DbSet) - something like this http://stackoverflow.com/questions/9892690/dynamic-lambda-expression-for-filtering - but it is much convenient to say ctx.MyEntities.Where(e => e.Id == 5). – Pawel Oct 23 '12 at 18:44
  • Wrong link for the dynamic Where - here is the correct link: http://stackoverflow.com/questions/10402029/ef-object-comparison-with-generic-types – Pawel Oct 23 '12 at 18:48
1

An Expression is code representing code. It is not an executable piece of code that you can run until you call Compile().

An Expression contains an object graph that describes some code. So the example function:

x => x + 1

Is representing a function that takes 1 parameter, and returns the expression (x + 1). As an object graph, it's something like this:

Expression
   AdditionExpression
       RightValue
          VariableExpression (x)
       LeftValue
          LiteralExpression (1)

You could parse through the expression and figure out the name of the parts of the expression, or what the literal value is, or what the operation is. Once it is compiled, it is a logical sequence of operations, and all of that metadata is lost.

In terms of Entity Framework, given a sample expression like so:

myObjects.Where(x => x.Id > 10).ToList()

The LINQ to SQL IQueryable provider is inspecting this expression, finding the name of the property Id and then finding the literal value of 10 to convert that into a SQL statement. If this was a function (a compiled unit of code), there is no metadata to inspect, and conversion to SQL would be impossible. It also allows you to deconstruct things into supported and unsupported expressions for efficiency in making sure your SQL query returns the requested data only, instead of a large data set.

Tejs
  • 40,736
  • 10
  • 68
  • 86