Duration (Pacific Standard Time):
To (Pacific Standard Time):
Impact:
  • None
User Action:
  • None
.NET Framework Class Library

DataContext.GetCommand Method

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

Namespace:  System.Data.Linq
Assembly:  System.Data.Linq (in System.Data.Linq.dll)
Public Function GetCommand ( _
	query As IQueryable _
) As DbCommand

Parameters

query
Type: System.Linq.IQueryable

The query whose SQL command information is to be retrieved.

Return Value

Type: System.Data.Common.DbCommand
The requested command information object.

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.

' 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(vbNewLine & "Command Text: " & vbNewLine & dc.CommandText)
Console.WriteLine(vbNewLine & "Command Type: {0}", dc.CommandType)
Console.WriteLine(vbNewLine & "Connection: {0}", dc.Connection)

Console.ReadLine()

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4