Sql generated by Linq


Hello, I would like to know how to view the sql code that a linq expression generates to be run by ADO.Net.

I'm using NHibernate as an ORM framework.

asked by anonymous 03.03.2016 / 20:49

1 answer


Make a function like this

public string GetGeneratedSql(System.Linq.IQueryable queryable, ISession session)
    var sessionImp = (ISessionImplementor) session;
    var nhLinqExpression = new NhLinqExpression(queryable.Expression, sessionImp.Factory);
    var translatorFactory = new ASTQueryTranslatorFactory();
    var translators = translatorFactory.CreateQueryTranslators(nhLinqExpression, null, false, sessionImp.EnabledFilters, sessionImp.Factory);

    return translators[0].SQLString;


03.03.2016 / 20:53