views:

35

answers:

1

Can NHibernate linq/lambda expressions be compiled so they aren't reevaluate on every use?

+2  A: 

Compiling them (into delegates) would make them execute in memory, which is something you definitely do NOT want.

They must stay as expression trees in order to be parsed into Criteria expressions (2.x contrib provider) or HQL trees (3.x provider), and then into SQL.

Diego Mijelshon
Why would it make them execute in memory? If all parameters are properly lifted into the delegate, it shouldn't.
Mauricio Scheffer
By "compile" I understand calling `Expression<TDelegate>.Compile()`. After doing that, you no longer have an expression tree; you have MSIL. And, unless you decompile MSIL (unlikely), you can't transform that into SQL for server-side execution.
Diego Mijelshon
@Diego Mijelshon: yes, you're right. Still, it should be possible in principle to build a pre-compiler for NH linq queries just like System.Data.Linq's CompiledQuery (http://msdn.microsoft.com/en-us/library/bb548737.aspx) which takes an Expression and returns a delegate. It's just that it's not as trivial as doing `Expression<TDelegate>.Compile()`.
Mauricio Scheffer
I reflected CompiledQuery and it seems to just call Compile()
bleevo
@bleevo: drill down into CompiledQuery.Compile, it's a lot more complex than that.
Mauricio Scheffer
@bleevo: I think the actual implementation of that is in System.Data.Linq.SqlClient.SqlProvider.Compile().
Mauricio Scheffer