4

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

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Why would it make them execute in memory? If all parameters are properly lifted into the delegate, it shouldn't.
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: 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 (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().
I reflected CompiledQuery and it seems to just call Compile()
@bleevo: drill down into CompiledQuery.Compile, it's a lot more complex than that.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.