有关从 linq 提取 SQL 命令的另一种方法,请参阅https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql/linq/how-to-display-linq-to-sql-commands。
更准确地说,以下代码段显示了用例
Northwnd db = new Northwnd(@"c:\northwnd.mdf");
var q =
from cust in db.Customers
where cust.City == "London"
select cust;
DbCommand dc = db.GetCommand(q);
Console.WriteLine("\nCommand Text: \n{0}",dc.CommandText);
Console.WriteLine("\nCommand Type: {0}",dc.CommandType);
Console.WriteLine("\nConnection: {0}",dc.Connection);
Console.ReadLine();
给出一个输出
Command Text:
SELECT [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName], [t0].[ContactT
itle], [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Coun
try], [t0].[Phone], [t0].[Fax]
FROM [dbo].[Customers] AS [t0]
WHERE [t0].[City] = @p0
Command Type: Text
Connection: System.Data.SqlClient.SqlConnection