In this chapter we’ll start querying the data in our carefully designed models.
There are lots (and lots) of books on LINQ. That’s only fair; it’s a huge subject. But in reality, 99% of what you need to do is really pretty simple. Most of the time you just want to retrieve some data, usually filtered according to some criteria you’ll determine at runtime. We’ll start the chapter by looking at those kinds of simple operations.
LINQ to Entities is only available in C#, Visual Basic and F# (with the F# Power Pack). There is another way of querying an EDM, however: Entity SQL. Entity SQL has some drawbacks compared to LINQ, not the least of which is that it’s only exposed directly on the ObjectContext
(not the DbContext
), but there are a few things that are easier, like parameterized queries, and we’ll examine them in the next section.
We’ll conclude the chapter with a look at the special query functions exposed by the DbContext
and ObjectContext
APIs. The DbContext API (specifically the DbSet<T>
class) provides functionality to easily control roundtrips to the database, while the ObjectContext
exposes a method, ExecuteStorecommand()
, which allows you to use stored procedures defined by the database.