Using LinqToSQL is fun and speeds up a lot of things. However, there are issues with LinqToSQL which confuse people. Ultimately, some of these people tend to think that LinqToSQL is useless because things that should be obvious are not such obvious. Over a year ago I wrote a short blog entry on one of such basic issues.
Another such "not-so-obvious" thing is the issue of "ordering". One of the common requirements is to be able to order by the name of the property and not by the property itself. For example, if you use ObjectDataSource, then you know that the sorting parameter is passed to the object responsible for data retrieving by name of the parameter.
Let's take a look at two LinqToSQL ordering attempts:
In the example code above, the first linq clause is obvious - the ordering uses the property "Property" in a direct way. However, in in the second clause we use the name of the property.
You are to answer two following questions:
1. Why the second clause does not produce correct results, although it compiles correctly?
2. How is it possible then to build generic linq expressions which sort objects by names of their properties.
By generic I mean that following solution is not acceptable: