Interpreted Queries
LINQ provides two parallel architectures:
local queries for local object collections, and
interpreted queries for remote data sources. So
far, we’ve examined the architecture of local queries, which operate over collections implementing
IEnumerable<>
. Local queries
resolve to query operators in the Enumerable
class, which in turn resolve to
chains of decorator sequences. The delegates that they accept—whether
expressed in comprehension syntax, lambda syntax, or traditional
delegates—are fully local to Intermediate Language (IL) code just as any
other C# method.
By contrast, interpreted queries are
descriptive. They operate over sequences that
implement IQueryable<>
, and
they resolve to the query operators in the Queryable
class, which emit
expression trees that are interpreted at
runtime.
Note
The query operators in Enumerable
can actually work with IQueryable<>
sequences. The difficulty
is that the resultant queries always execute locally on the
client—this is why a second set of query operators is provided in the
Queryable
class.
There are two IQueryable
implementations in the .NET Framework:
LINQ to SQL
LINQ to Entities
In addition, the AsQueryable
extension method generates an IQueryable
wrapper around an ordinary
enumerable collection. We describe AsQueryable
in the upcoming “Building Query
Expressions” section.
In this section, we’ll use LINQ to SQL to illustrate interpreted query architecture.
Note
IQueryable<>
is an
extension of IEnumerable<>
with additional methods ...
Get LINQ Pocket Reference 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.