DataContext.GetCommand(IQueryable) 方法


取得由 LINQ to SQLLINQ to SQL 產生之有關 SQL 命令的資訊。Gets the information about SQL commands generated by LINQ to SQLLINQ to SQL.

 System::Data::Common::DbCommand ^ GetCommand(System::Linq::IQueryable ^ query);
public System.Data.Common.DbCommand GetCommand (System.Linq.IQueryable query);
member this.GetCommand : System.Linq.IQueryable -> System.Data.Common.DbCommand
Public Function GetCommand (query As IQueryable) As DbCommand



會擷取內含 SQL 命令資訊的查詢。The query whose SQL command information is to be retrieved.


要求的命令資訊物件。The requested command information object.


// using System.Data.Common;
Northwnd db = new Northwnd(@"c:\northwnd.mdf");

var q =
    from cust in db.Customers
    where cust.City == "London"
    select cust;

Console.WriteLine("Customers from London:");
foreach (var z in q)
    Console.WriteLine("\t {0}",z.ContactName);

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);

' Imports System.Data.Common

Dim db As New Northwnd("c:\northwnd.mdf")

Dim q = _
From cust In db.Customers _
Where cust.City = "London" _
Select cust

Console.WriteLine("Customers from London:")
For Each z As Customer In q
    Console.WriteLine(vbTab & z.ContactName)

Dim dc As DbCommand = db.GetCommand(q)
Console.WriteLine(vbNewLine & "Command Text: " & vbNewLine & dc.CommandText)
Console.WriteLine(vbNewLine & "Command Type: {0}", dc.CommandType)
Console.WriteLine(vbNewLine & "Connection: {0}", dc.Connection)



這個方法只是 getter,不會影響DataContext狀態。This method is only a getter and does not affect DataContext state.

請注意下列考慮:Note the following considerations:

  • 引數必須為非 null。The argument must be non-null. 否則,會擲回 null 引數例外狀況。Otherwise, a null argument exception is thrown.

  • 查詢執行期間LINQ to SQLLINQ to SQL擲回的一般查詢轉譯例外狀況適用于無法轉譯的查詢。Normal query translation exceptions thrown during LINQ to SQLLINQ to SQL query execution apply for a query that cannot be translated.

  • 只會傳回第一個查詢命令。Only the first query command is returned. 具體而言,不包含用於積極式載入(LoadWith)的其他命令。Specifically, additional commands that are used for eager loading (LoadWith) are not included.

  • DataContext不會追蹤使用者使用命令執行的工作。DataContext does not track what the user does with the command. 例如,傳回的命令執行結果不會被追蹤,而且不會影響DataContext狀態。For example, results from the execution of the returned command are not tracked and do not affect DataContext state.