久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

從 LINQ 表達式中提取 sql 查詢

Extract sql query from LINQ expressions(從 LINQ 表達式中提取 sql 查詢)
本文介紹了從 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;

替代文字 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 的 TextWriters 允許您將輸出發送到調試器輸出窗口.

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來自 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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Why shouldn#39;t I always use nullable types in C#(為什么我不應該總是在 C# 中使用可空類型)
C# HasValue vs !=null(C# HasValue vs !=null)
C# ADO.NET: nulls and DbNull -- is there more efficient syntax?(C# ADO.NET:空值和 DbNull —— 有沒有更高效的語法?)
How to set null value to int in c#?(如何在c#中將空值設置為int?)
How to handle nulls in LINQ when using Min or Max?(使用 Min 或 Max 時如何處理 LINQ 中的空值?)
Method call if not null in C#(在 C# 中如果不為 null 的方法調用)
主站蜘蛛池模板: 91精品国产综合久久国产大片 | 香蕉久久久久久 | 亚洲视频免费观看 | 国产在线观看一区二区 | 在线观看免费黄色片 | 九九久久这里只有精品 | 日本视频免费观看 | 日本特黄特色aaa大片免费 | 91一区二区在线观看 | www性色 | 久久久久久久久91 | 中国一级特黄真人毛片免费观看 | 欧美日韩中文在线 | 看片wwwwwwwwwww| 天天碰夜夜操 | 久久国产成人 | 亚洲成人精品久久久 | 成人在线视频网址 | 欧美另类视频在线 | 九九热精品在线 | 亚洲少妇综合网 | 日本精品视频一区二区三区四区 | 欧美日韩电影一区二区 | 91精品国产91久久综合桃花 | 日本视频免费 | 日韩一区二区三区av | 337p日本欧洲亚洲大胆鲁鲁 | 国产一二三区在线 | 欧美一级片在线观看 | 亚洲日本一区二区 | 久久33| 久久a久久| japanhdxxxx裸体 | 亚洲精品视频免费观看 | 欧美一区在线看 | 观看毛片 | 伊人青青久久 | 久久精品一区 | 九色网址 | 亚洲自拍偷拍免费视频 | 久久久综合精品 |