The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

matte (2) [Avatar] Offline
Great book so far...

Re the two code snippets below

I know it might just be a question of preference, but I find the second one much more readable.
Could you also just describe how the lambda values (item, type) are interpreted and resolved?

var types =
.Select(item => item.GetType().Name)
.OrderBy(type => type);

as opposed to (my expression syntax version)

var types = from type in array
orderby (type.GetType().Name)
select (type.GetType().Name);
fabrice.marguerie (224) [Avatar] Offline
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:
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:
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.

matte (2) [Avatar] Offline
Re: Chapter 4. Listing 4.2
Thanks for your response.

I take your points re the semantics of the two techniques.

Can you shed a little light on the second half of the question

How does one interpret :

.OrderBy (type=> type)

I'm having a little trouble understanding it...

thanks again.
fabrice.marguerie (224) [Avatar] Offline
Re: Chapter 4. Listing 4.2
The call to Select selects the name of each item's type -> String, Int32, Boolean, Char
The call to OrderBy simply sorts this list of type names -> Boolean, Char, Int32, String