Using SQL Syntax Against Entities

LINQ to SQL also allows writing SQL code against entities so that you can still take advantage of the object model if you prefer the old-fashioned way of manipulating data. The DataContext class offers an instance method named ExecuteQuery(Of T) that allows sending SQL instructions in string form. For example, the following code retrieves a list of products for the Grain/Cereals category, ordered by product name:

image

ExecuteQuery(Of T) returns an IEnumerable(Of T) that you can then treat as you like, according to LINQ specifications. You can also send SQL instructions directly to the database invoking the ExecuteCommand ...

Get Visual Basic® 2010 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.