Direct SQL
LINQ to Entities is a part of the ADO.NET family of technologies. It is based on services provided by the ADO.NET provider model. Therefore it is possible to mix LINQ to Entities code with existing ADO.NET applications.
In some cases you might find that the query or submit changes facility of ObjectContext
is insufficient for the specialized task that you want to perform. In these cases it is possible to use ObjectContext
to issue raw SQL commands directly to the database.
The ExecuteStoreQuery()
method lets you execute a raw SQL query and converts the result of your query directly into objects.
The ExecuteStoreCommand()
method lets you directly execute SQL commands against the database.
For example, the following code will retrieve all discontinued products and update the price for one product:
var products = NWEntities.ExecuteStoreQuery<Product>( '"SELECT * '" + '"FROM Products '" + '"WHERE Discontinued = 0 '" + '"ORDER BY ProductName;'" ); Console.WriteLine...