13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Translations us<strong>in</strong>g IQueryable and IQueryProvider333Count, or Average, we’re no longer really creat<strong>in</strong>g a “source”—we’re evaluat<strong>in</strong>g a resultimmediately. That’s when Execute is called, as shown by list<strong>in</strong>g 12.9 and its output.List<strong>in</strong>g 12.9 IQueryProvider.Executevar query = from x <strong>in</strong> new FakeQuery()where x.StartsWith("abc")select x.Length;double mean = query.Average();// OutputFakeQueryProvider.CreateQueryExpression=FakeQuery.Where(x => x.StartsWith("abc"))FakeQueryProvider.CreateQueryExpression=FakeQuery.Where(x => x.StartsWith("abc")).Select(x => x.Length)FakeQueryProvider.ExecuteExpression=FakeQuery.Where(x => x.StartsWith("abc")).Select(x => x.Length).Average()The FakeQueryProvider can be quite useful when it comes to understand<strong>in</strong>g what the<strong>C#</strong> compiler is do<strong>in</strong>g beh<strong>in</strong>d the scenes with query expressions. It will show the transparentidentifiers <strong>in</strong>troduced with<strong>in</strong> a query expression, along with the translated callsto SelectMany, GroupJo<strong>in</strong>, and the like.12.2.5 Wrapp<strong>in</strong>g up IQueryableWe haven’t written any of the significant code that a real query provider would need<strong>in</strong> order to get useful work done, but hopefully our fake provider has given you<strong>in</strong>sight <strong>in</strong>to how LINQ providers are given the <strong>in</strong>formation from query expressions.It’s all built up by the Queryable extension methods, given an appropriate implementationof IQueryable and IQueryProvider.We’ve gone <strong>in</strong>to a bit more detail <strong>in</strong> this section than we will for the rest of thechapter, as it’s <strong>in</strong>volved the foundations that underp<strong>in</strong> the LINQ to SQL code we sawearlier. You’re unlikely to want to write your own query provider—it takes a lot of workto produce a really good one—but this section has been important <strong>in</strong> terms of conceptualunderstand<strong>in</strong>g. The steps <strong>in</strong>volved <strong>in</strong> tak<strong>in</strong>g a <strong>C#</strong> query expression and (atexecution time) runn<strong>in</strong>g some SQL on a database are quite profound and lie at theheart of the big features of <strong>C#</strong> 3. Understand<strong>in</strong>g why <strong>C#</strong> has ga<strong>in</strong>ed these features willhelp keep you more <strong>in</strong> tune with the language.In fact, LINQ to SQL is the only provider built <strong>in</strong>to the framework that actually usesIQueryable—the other “providers” are just APIs that play nicely with LINQ to Objects.I don’t wish to dim<strong>in</strong>ish their importance—they’re still useful. However, it does meanthat you can relax a bit now—the hardest part of the chapter is beh<strong>in</strong>d you. We’re stillstay<strong>in</strong>g with database-related access for our next section, though, which looks at LINQto DataSet.Licensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!