DataContext.GetCommand(IQueryable) Method

Definition

Gets the information about SQL commands generated by LINQ to SQL.

public:
 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

Parameters

query
IQueryable

The query whose SQL command information is to be retrieved.

Returns

The requested command information object.

Examples

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

Console.ReadLine();
' 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)
Next

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

Console.ReadLine()

Remarks

This method is only a getter and does not affect DataContext state.

Note the following considerations:

  • The argument must be non-null. Otherwise, a null argument exception is thrown.

  • Normal query translation exceptions thrown during LINQ to SQL query execution apply for a query that cannot be translated.

  • Only the first query command is returned. Specifically, additional commands that are used for eager loading (LoadWith) are not included.

  • DataContext does not track what the user does with the command. For example, results from the execution of the returned command are not tracked and do not affect DataContext state.

Applies to