public static string ToSql <TEntity>(this IQueryable <TEntity> query) where TEntity : class { QueryCompiler queryCompiler = (QueryCompiler)QueryCompilerField.GetValue(query.Provider); QueryModelGenerator modelGenerator = (QueryModelGenerator)QueryModelGeneratorField.GetValue(queryCompiler); Remotion.Linq.QueryModel queryModel = modelGenerator.ParseQuery(query.Expression); IDatabase database = (IDatabase)DataBaseField.GetValue(queryCompiler); DatabaseDependencies databaseDependencies = (DatabaseDependencies)DatabaseDependenciesField.GetValue(database); QueryCompilationContext queryCompilationContext = databaseDependencies.QueryCompilationContextFactory.Create(false); RelationalQueryModelVisitor modelVisitor = (RelationalQueryModelVisitor)queryCompilationContext.CreateQueryModelVisitor(); modelVisitor.CreateQueryExecutor <TEntity>(queryModel); string sql = modelVisitor.Queries.First().ToString(); return(sql); }
public static RelationalQueryModelVisitor CompileQuery <TEntity>(this IQueryable <TEntity> self) { var q = self as EntityQueryable <TEntity>; if (q == null) { return(null); } var fields = typeof(Database).GetTypeInfo().DeclaredFields; var queryModelGenerator = new QueryModelGenerator(new DefaultMethodInfoBasedNodeTypeRegistryFactory(), new EvaluatableExpressionFilter()); var queryCompiler = (QueryCompiler)ReflectionCommon.QueryCompilerOfEntityQueryProvider.GetValue(self.Provider); var database = (Database)ReflectionCommon.DatabaseOfQueryCompiler.GetValue(queryCompiler); var dependencies = (DatabaseDependencies)ReflectionCommon.DependenciesOfDatabase.GetValue(database); var factory = dependencies.QueryCompilationContextFactory; var queryModel = queryModelGenerator.ParseQuery(self.Expression); var modelVisitor = (RelationalQueryModelVisitor)database.CreateVisitor(factory, queryModel); modelVisitor.CreateQueryExecutor <TEntity>(queryModel); return(modelVisitor); }