0

Assuming that I have setup NHibernate data context with all relations, tables, connection etc.

How I would convert an instantiated .NET Expression (actually it's not a full SELECT but just a WHERE expression) to an SQL string?

I've seen many references. Like using ExpressionVisitor or building IQueryable Provider, but I believe NHibernate 3 has all these implementations and I just have to combine some classes to achieve the goal. Ant ideas?

  • Which specific version of NHibernate are you using? Last time I checked (about a 1.5 years ago), even different 3.x versions had some significant API differences. –  Apr 05 '14 at 00:54
  • @Cupcake I use version 3.3.1.4000. Actually I found the solution and posted the answer. Although translating the part of Expression to SQL doesn't work. – Eugene Tiutiunnyk Apr 05 '14 at 01:18

2 Answers2

0

Well, right after posting this question an approach was found here: How can I have NHibernate only generate the SQL without executing it?.

Actually it's possible to get a text only for the full SELECT query, otherwise the exception is raised:

Cannot parse expression '(rec.Id == 6)' as it has an unsupported type. Only query sources (that is, expressions that implement IEnumerable) and query operators can be parsed.

Community
  • 1
  • 1
0

See my other answer about how to intercept sql queries by writing a custom batchers: NHibernate and interceptors - measuring/monitoring SQL round-trip times It works fine for Sql Server too, once you intercept the query you are free to prevent it from executing by returning an empty result.

Community
  • 1
  • 1
Onur Gumus
  • 1,389
  • 11
  • 27
  • Hey @ReverseBlade thanks for your comment, although I am not trying to intercept nHibernate or see the queries that are being sent to DB. I just wanted to translate part of Expression query (WHERE section) to SQL text. It seems that it's impossible with nHibernate since only enumerable expressions could be translated. – Eugene Tiutiunnyk Apr 07 '14 at 05:36