提取LINQ表达式sql查询 [英] Extract sql query from LINQ expressions

查看:139
本文介绍了提取LINQ表达式sql查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能从LINQ查询中提取的SQL语句?

我说,我有这样的LINQ表达式。

Say, I have this LINQ expression.

        string[] names =
            new string[] { "Jon Skeet", "Marc Gravell", "tvanfosson", 
                           "cletus", "Greg Hewgill", "JaredPar" };

        var results = from name in names
                      where name.StartsWith("J")
                      select name;



替换文本

在此声明'结果'只抱着LINQ的表达,而不是结果,由于LINQ查询的延迟执行。

After this statement 'results' is only holding the LINQ expression and not the results due to deferred execution of LINQ queries.

我提取或产生LINQ查询出来的结果和
准备从一个有效的SQL语句
查询存储在LINQ'?

下面是我的目标:

我们已经写我们自己的ORM。我们必须编写查询每一次,我们需要做数据库操作。现在我们需要在DAL摆脱它。我们希望写代码,会产生SQL语句对我的ORM,我们将在数据库上执行此SQL LINQ表达式。

We have written our own ORM. We have to write queries every-time we need to do db operations. Now we need to get rid of it at DAL. We wish to write LINQ expression in code which will produce SQL statements against my ORM and we will execute this SQL on the database.

将我haev写我?自定义LINQ提供做什么,我需要

推荐答案

编辑:等等,你在谈论LINQ到对象?不,那是不可能的 1 。有没有办法转换的表达式树的LINQ到对象查询,表示查询一些数据库的表达式树

Wait, you're talking about LINQ to Objects? No, that is impossible1. There is no way to convert the expression tree for a LINQ to Object query to an expression tree that represents querying some database.

编辑:不要写你自己ORM。有这个问题行之有效的解决方案。你试图再次解决这个问题浪费值。如果你打算使用自己的ORM,要转换为SQL语句的表达,是你将不得不编写自己的供应商。这里有一个演练 MSDN上这样做。

Don't write you're own ORM. There are proven solutions to this problem. You're wasting value by trying to solve this problem again. If you're going to use your own ORM, to translate an expression to a SQL statement, yes you will have to write your own provider. Here's a walkthrough on MSDN doing that.

但严重的是,不写自己的ORM。五年前,NHibernate和LINQ到SQL之前来了,成熟了,挺好的。但是不是现在。没办法。

But seriously, do not write your own ORM. Five years ago before NHibernate and LINQ to SQL came along and matured, fine. But not now. No way.

这个答案的其余部分假定你询问LINQ到SQL。

The rest of this answer assumed that you were asking about LINQ to SQL.

有。两种方法,我知道的。

There are two ways that I know of.

首先:

设置的 DataContext.Log 属性 Console.Out (或其他 System.IO.TextWriter ):

var db = new MyDataContext();
db.Log = Console.Out;

这为在执行将打印SQL语句到控制台。欲了解更多关于此功能请参见 MSDN 。在其他地方也有的的TextWriter 例子。code>取值,让您将输出发送到调试器输出窗口

This will print the SQL statements to the console as they are executed. For more on this feature see MSDN. Elsewhere there are examples of TextWriters that let you send the output to the debugger output window.

二:

从使用的LINQ to SQL调试展台斯科特格思里。这使您可以通过Visual Studio中的调试器看到的SQL语句。此选项的优点是,你可以看到该SQL语句而不执行查询。你甚至可以执行查询,并看到在可视化的结果。

Use the LINQ to SQL Debug Visualizer from Scott Guthrie. This allows you to see the SQL statements through the debugger in Visual Studio. This option has the advantage that you can see the SQL statement without executing the query. You can even execute the query and see the results in the visualizer.

1 :也许不是不可能,但肯定很辛苦。

1: Perhaps not impossible, but certainly very hard.

这篇关于提取LINQ表达式sql查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆