Επεξεργασία

Κοινή χρήση μέσω


DataContext.ExecuteQuery Method

Definition

Overloads

ExecuteQuery(Type, String, Object[])

Executes SQL queries directly on the database.

ExecuteQuery<TResult>(String, Object[])

Executes SQL queries directly on the database and returns objects.

ExecuteQuery(Type, String, Object[])

Executes SQL queries directly on the database.

public:
 System::Collections::IEnumerable ^ ExecuteQuery(Type ^ elementType, System::String ^ query, ... cli::array <System::Object ^> ^ parameters);
public System.Collections.IEnumerable ExecuteQuery (Type elementType, string query, params object[] parameters);
member this.ExecuteQuery : Type * string * obj[] -> System.Collections.IEnumerable
Public Function ExecuteQuery (elementType As Type, query As String, ParamArray parameters As Object()) As IEnumerable

Parameters

elementType
Type

The type of the IEnumerable<T> to be returned.

The algorithm for matching columns in the result of the query to fields or properties in the object works as follows:

If a field or property is mapped to a particular column name, that column name is expected in the resultset.

If a field or property is not mapped, a column with the same name as the field or property is expected in the resultset.

The comparison is performed by looking for a case-sensitive match first. If this match is not found, a subsequent search occurs for a case-insensitive match.

The query must return all the tracked fields and properties of the object (except those that are loaded on a deferred basis) when all the following conditions are true:

T is an entity explicitly tracked by the DataContext.

ObjectTrackingEnabled is true.

The entity has a primary key.

Otherwise an exception is thrown.

query
String

The SQL query to be executed.

parameters
Object[]

The array of parameters to be passed to the command. Note the following behavior:

If the number of objects in the array is less than the highest number identified in the command string, an exception is thrown.

If the array contains objects that are not referenced in the command string, no exception is thrown.

If a parameter is null, it is converted to DBNull.Value.

Returns

An IEnumerable<T> collection of objects returned by the query.

Applies to

ExecuteQuery<TResult>(String, Object[])

Executes SQL queries directly on the database and returns objects.

public:
generic <typename TResult>
 System::Collections::Generic::IEnumerable<TResult> ^ ExecuteQuery(System::String ^ query, ... cli::array <System::Object ^> ^ parameters);
public System.Collections.Generic.IEnumerable<TResult> ExecuteQuery<TResult> (string query, params object[] parameters);
member this.ExecuteQuery : string * obj[] -> seq<'Result>
Public Function ExecuteQuery(Of TResult) (query As String, ParamArray parameters As Object()) As IEnumerable(Of TResult)

Type Parameters

TResult

The type of the elements in the returned collection.

Parameters

query
String

The SQL query to be executed.

parameters
Object[]

The array of parameters to be passed to the command. Note the following behavior:

If the number of objects in the array is less than the highest number identified in the command string, an exception is thrown.

If the array contains objects that are not referenced in the command string, no exception is thrown.

If a parameter is null, it is converted to DBNull.Value.

Returns

IEnumerable<TResult>

A collection of objects returned by the query.

Examples

The following example shows one use for this method:

var customers = db.ExecuteQuery<Customer>(@"SELECT CustomerID, CompanyName, ContactName, ContactTitle,
   Address, City, Region, PostalCode, Country, Phone, Fax  
   FROM   dbo.Customers
   WHERE  City = {0}", "London");

foreach (Customer c in customers)
    Console.WriteLine(c.ContactName);
Dim customers = db.ExecuteQuery(Of Customer)("SELECT CustomerID, _    
    CompanyName, ContactName, ContactTitle, _
   Address, City, Region, PostalCode, Country, Phone, Fax _
   FROM dbo.Customers _
WHERE City = {0}", "London")

For Each c As Customer In customers
    Console.WriteLine(c.ContactName)
Next

Remarks

This method is a pass-through mechanism for cases where LINQ to SQL does not provide for a particular scenario.

The algorithm for matching columns in the result of the query to fields and properties in the object works as follows:

  • If a field or property is mapped to a particular column name, that column name is expected in the resultset.

  • If a field or property is not mapped, a column with the same name as the field or property is expected in the resultset.

  • The comparison is performed by first looking for a case-sensitive match. If such a match is not found, a subsequent search occurs for a case-insensitive match.

  • The query must return all the tracked fields and properties of the object (apart from those subject to deferred loading) when all the following are true:

    • If <T> is an entity explicitly tracked by the DataContext.

    • ObjectTrackingEnabled is true.

    • The entity has a primary key.

    Otherwise an exception is thrown.

  • In all other cases, the query can retrieve just a subset of the tracked fields and properties for the object.

Applies to