Query Expressions


LINQ offers several standard query operators, as you’ve seen. The declarative query expression that was used is resolved to the use of query operators. Once again have a look at the LINQ query to get racers from Brazil ordered by the number of wins:

 var queryBrazilRacers = from r in racers                         where r.Country == "Brazil"                         orderby r.Wins descending                         select r;

This query expression is resolved to the invoke of methods as shown here:

  var queryBrazilRacers = racers.Where<Racer>(r => r.Country == "Brazil").                         OrderByDescending<Racer, int>(r => r.Wins)                         Select(r => r); 

where from the query expression is resolved to calling the Where query operator. orderby descending becomes the query operator OrderByDescending. Ignore the expression within the brackets for the moment, that’s a lambda expression. Ignoring that expression, the query operators do look like normal methods. However, with the object list, the type of racers is a collection. Collections don’t support the methods Where(), OrderByDescending(), Select() - or do they?

The answer to this is that query operators are implemented as extension methods. Extension methods are a new syntax extension to C# 3.0.




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net