Query Expressions Are Just Method Invocations

Somewhat surprisingly, adding query expressions4 required no changes to the Common Language Runtime (CLR) or to the Common Intermediate Language (CIL). Rather, the C# compiler simply translates query expressions into a series of method calls. Consider, for example, the query expression from Listing 16.1, a portion of which appears in Listing 16.16.

Listing 16.16: Simple Query Expression
private static void ShowContextualKeywords1()
    IEnumerable<string> selection =
        from word in CSharp.Keywords
        where !word.Contains('*')
        select word;
    // ...

After compilation, the expression from Listing 16.16 is converted to an IEnumerable<T> extension method call from System.Linq.Enumerable, as shown in Listing 16.17.

Listing 16.17: Query Expression Translated to Standard Query Operator Syntax
private static void ShowContextualKeywords2()
    IEnumerable<string> selection =
        CSharp.Keywords.Where(word => !word.Contains('*'));
    // ...

As discussed in Chapter 15, the lambda expression is then itself translated by the compiler to emit a method with the body of the lambda, and its usage entails allocation of a delegate to that method.

Every query expression can (and must) be translated into method calls, but not every sequence of method calls has a corresponding query expression. For example, there is no query expression equivalent for the extension method TakeWhile<T>(Func<T, bool> predicate), which repeatedly returns items from the collection as long as the predicate returns true.

For those queries that do have both a method call form and a query expression form, which is better? This is a judgment call. Some queries are better suited for query expressions, whereas others are more readable as method invocations.

DO use query expression syntax to make queries easier to read, particularly if they involve complex from, let, join, or group clauses.
CONSIDER using the standard query operators (method call form) if the query involves operations that do not have a query expression syntax, such as Count(), TakeWhile(), or Distinct().
Chapter 16: Summary

This chapter introduced a new syntax—namely, query expressions. Readers familiar with SQL will immediately see the similarities between query expressions and SQL. However, query expressions also introduce additional functionality, such as grouping into a hierarchical set of new objects, which is unavailable with SQL. All of the functionality of query expressions was already available via standard query operators, but query expressions frequently provide a simpler syntax for expressing such a query. Whether standard query operators or query expression syntax is used, however, the end result is a significant improvement in the way developers can code against collection APIs—an improvement that ultimately provides a paradigm shift in the way object-oriented languages are able to interface with relational databases.

In the next chapter, we continue our discussion of collections by investigating some of the .NET framework collection types and exploring how to define custom collections.


4. Starting in C# 3.0.
{{ snackbarMessage }}