Before we used:

public string GetSqlForQuery<T>(IQueryable<T> query)
  if (Session.Current == null)
    throw new InvalidOperationException("A session is needed for thsi operation");

  var recordSet = ((Xtensive.Orm.Linq.Queryable<T>)query).Compiled;
  var executableProvider = Xtensive.Storage.Rse.Compilation.CompilationContext.Current.Compile(recordSet.Provider);
  Xtensive.Storage.Providers.Sql.SqlProvider sqlProvider = executableProvider as Xtensive.Storage.Providers.Sql.SqlProvider;
  var sessionHandler = Session.Current.Services.Demand<Xtensive.Storage.Providers.SessionHandler>();
  var domainHandler = sessionHandler.Handlers.DomainHandler;
  var compiled = sqlProvider.Request.GetCompiledStatement((Xtensive.Storage.Providers.Sql.DomainHandler)domainHandler);
  return compiled.ToString();

What's the 4.4 equivalent?

asked Feb 25 '11 at 11:41

olorin's gravatar image


One Answer:

As far as I understand, you need a service that translates a LINQ query into the corresponding SQL string representation. We'll try making the desired service soon so you won't need to mess with all stuff you've mentioned in the question.


In revision 7268 we've introduced the QueryFormatter service to help you sort out the task. Here is how it is intended to be used:

var formatter = session.Services.Get<QueryFormatter>();
string sql = formatter.ToSqlString(query); // Formats to SQL

one more option that might be useful:

string result = formatter.ToString(query); // Formats query to C# notation

Note, the API is not final, it might be subject for change.

answered Feb 28 '11 at 03:51

Dmitri%20Maximov's gravatar image

Dmitri Maximov

edited Mar 02 '11 at 11:36

Ok : thank you! For the record we use this to generate views for each persistent type, because it's easier for debug.

(Mar 02 '11 at 07:46) olorin olorin's gravatar image

Nice approach, though.

(Mar 02 '11 at 09:34) Dmitri Maximov Dmitri%20Maximov's gravatar image

I've updated the answer.

(Mar 02 '11 at 11:36) Dmitri Maximov Dmitri%20Maximov's gravatar image

The binaries are published.

See 'Nightly builds' section on the downloads page.

(Mar 05 '11 at 11:36) Dmitri Maximov Dmitri%20Maximov's gravatar image

Thanks a lot! I'll test it ASAP.

(Mar 11 '11 at 10:55) olorin olorin's gravatar image
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA