LINQ: declarative and functional syntax

In my last post about LINQ I shown you an example about how to use the language integrated query to select information about directories. In this post I’ll get more into the syntax and show you something about the functional querying style.

My examples will focus on displaying information about UEFA Champions Leage winners. Thus, I have created a class called Winner that looks this this:

Also I have created a utility class that returns a list (incomplete) of UCL winners:

Now, let’s see how we could display all this info ascending by the year of winning:

That lists the following:

This SQL-like syntax is however only a “shell” over the functional syntax. It’s just like with the foreach statement. To be able to iterate with foreach, the collection must implement IEnumerable, which has a method that returns a class that implements IEnumerator. What the compiler is doing when using foreach is calling GetEnumerator to get an iterator, and then Reset() on it, and inserts a while(iterator.MoveNext()), using Current to access the current object from the collection. The same happens here with the declarative query syntax.

Now, let’s suppose we want to list only the winners from England. What we have to do is adding a filtering:

What I wrote above is actually the same with:

Here we used the operators OrderBy, Where and Select. These are two of the query operators that allow you to perform filtering, projection and key extraction. These are built of the concept of Lambda expression, which are similar to the CLR delegates. We could rewrite the last query list this:

OrderBy and OrderByDescending are operators that impose a partial order over the keys. Operators ThenBy and ThenByDescending are used to apply additional sorting criteria by only on sorted sequences (SortedSequence).

Where is used to exclude items from the collection.

Select and SelectMany are operators for projecting only those fields or info that is wanted.

Now, let’s try something more complicated: grouping the winners by country, and inside each group ascending by the winning year. In declarative syntax that would be like this:

In functional programming syntax, the same query is written as:

The GroupBy operator imposes a partitioning over a sequence of values based on a key extraction function. It returns a sequence of Grouping values, which contains both the key as well as the group of values mapped to the key. The interface of Grouping is:

You may wonder why the declarative syntax starts with the from clause. The SQL language has a problem, i.e. the order of clauses is not natural. Select is the first by at the time of selecting, you don’t know where do you select from. The from clause is naturally the first one, and this was rectified in LINQ. You always have to put from as the first clause in a query (when using the declarative syntax). In the functional syntax you can see you have to apply the operators on a collection, a sequence of items (in our case winners). That is the functional equivalent of from.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.