問題描述
是否可以從 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;
替代文字 http://ruchitsurati.net/files/linq-debugging.png一個>
在此語句之后,results"僅保存 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 擺脫它.我們希望在代碼中編寫 LINQ 表達式,該表達式將針對我的 ORM 生成 SQL 語句,我們將在數據庫上執行此 SQL.
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.
我是否需要編寫自定義 Linq 提供程序來滿足我的需要?
推薦答案
等等,你在談論 LINQ to Objects?不,這是不可能的1.無法將 LINQ to Object 查詢的表達式樹轉換為表示查詢某個數據庫的表達式樹.
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 to 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 to SQL.
The rest of this answer assumed that you were asking about LINQ to SQL.
我知道有兩種方法.
首先:
設置DataContext.Log
屬性到 Console.Out
(或您選擇的另一個 System.IO.TextWriter
):
Set the DataContext.Log
property to Console.Out
(or another System.IO.TextWriter
of your choice):
var db = new MyDataContext();
db.Log = Console.Out;
這將在執行時將 SQL 語句打印到控制臺.有關此功能的更多信息,請參閱 MSDN.在其他地方有 examples 的 TextWriter
s 允許您將輸出發送到調試器輸出窗口.
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 TextWriter
s that let you send the output to the debugger output window.
第二:
使用 LINQ來自 Scott Guthrie 的 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 查詢的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!