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
2010-08-30 02:33:42
Why would it make them execute in memory? If all parameters are properly lifted into the delegate, it shouldn't.
Mauricio Scheffer
2010-08-30 02:39:48
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
2010-08-30 03:07:37
@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
2010-08-30 04:23:13
I reflected CompiledQuery and it seems to just call Compile()
bleevo
2010-08-30 04:41:43
@bleevo: drill down into CompiledQuery.Compile, it's a lot more complex than that.
Mauricio Scheffer
2010-08-30 12:42:28
@bleevo: I think the actual implementation of that is in System.Data.Linq.SqlClient.SqlProvider.Compile().
Mauricio Scheffer
2010-08-30 16:08:26