Export (0) Print
Expand All

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 DbCommand GetCommand(
	IQueryable query
)

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.

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft