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.

362 APPENDIX LINQ standard query operatorsTable A.3Conversion examples (cont<strong>in</strong>ued)Expression// Key is first character of wordwords.ToLookup(word => word[0])words.ToDictionary(word => word[0])ResultLookup contents:'z': "zero"'o': "one"'t': "two", "three"'f': "four"Exception: Can only have one entry per key, sofails on 't'I haven’t provided examples for AsEnumerable or AsQueryable because they don’taffect the results <strong>in</strong> an immediately obvious way. Instead, they affect the manner <strong>in</strong> whichthe query is executed. Queryable.AsQueryable is an extension method on IEnumerablethat returns an IQueryable (both types be<strong>in</strong>g generic or nongeneric, depend<strong>in</strong>gon which overload you pick). If the IEnumerable you call it on is already an IQueryable,it just returns the same reference—otherwise it creates a wrapper around the orig<strong>in</strong>alsequence. The wrapper allows you to use all the normal Queryable extension methods,pass<strong>in</strong>g <strong>in</strong> expression trees, but when the query is executed the expression tree is compiled<strong>in</strong>to normal IL and executed directly, us<strong>in</strong>g the LambdaExpression.Compilemethod shown <strong>in</strong> section 9.3.2.Enumerable.AsEnumerable is an extension method on IEnumerable and has atrivial implementation, simply return<strong>in</strong>g the reference it was called on. No wrappersare <strong>in</strong>volved—it just returns the same reference. This forces the Enumerable extensionmethods to be used <strong>in</strong> subsequent LINQ operators. Consider the follow<strong>in</strong>gquery expressions:// Filter the users <strong>in</strong> the database with LIKEfrom user <strong>in</strong> context.Userswhere user.Name.StartsWith("Tim")select user;// Filter the users <strong>in</strong> memoryfrom user <strong>in</strong> context.Users.AsEnumerable()where user.Name.StartsWith("Tim")select user;The second query expression forces the compile-time type of the source to beIEnumerable <strong>in</strong>stead of IQueryable, so all the process<strong>in</strong>g is done <strong>in</strong>memory <strong>in</strong>stead of at the database. The compiler will use the Enumerable extensionmethods (tak<strong>in</strong>g delegate parameters) <strong>in</strong>stead of the Queryable extension methods(tak<strong>in</strong>g expression tree parameters). Normally you want to do as much process<strong>in</strong>g aspossible <strong>in</strong> SQL, but when there are transformations that require “local” code, yousometimes have to force LINQ to use the appropriate Enumerable extension methods.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!