Re: Chapter 4. Listing 4.2
Hi Matthew,
Yes, in this case it's a matter of taste. You can use whatever syntax you prefer.
Two remarks though:
1) You can write the query expression with less parentheses:
[pre]
var types =
from type in array
orderby type.GetType().Name
select type.GetType().Name;[/pre]
2) The above query expression is not the exact translation of the chained query operators version. Here is the correct translation:
[pre]
var types =
from item in array
let name = itemGetType().Name
orderby name
select name;[/pre]
The difference is that in the latter query, type.GetType().Name gets executed only once per element. With the query expression you suggest, type.GetType().Name gets executed twice per element!
See, it's not that easy to convert queries from one syntax to another. Moreover, the query expression is now longer than the original query.
In other cases, you don't have the choice because not everything can be written as a query expression.See Chapter 3 for more information, especially section 3.4.4.
Fabrice
|