LINQ - Query syntax vs method chains & lambda [closed]
Solution 1:
To answer your question about translation, the query expression will always be translated based on the rules on 7.16 of the C# 4 spec (or the equivalent in the C# 3 spec). In the example where you're asking the question about the Name
property, that's not a matter of the query expression translation - it's what the Select
and Where
methods do with the delegates or expression trees they take as parameters. Sometimes it makes sense to do a projection before filtering, sometimes not.
As for little rules, I only have one: use whichever way is most readable for the query in question. So if the query changes and "which form is more readable" changes at the same time, change the syntax used.
If you're going to use LINQ you should be happy with either syntax, at the very least to read.
I tend to find that queries with multiple range variable (e.g. via SelectMany
or Join
, or a let
clause) end up being more readable using query expressions - but that's far from a hard and fast rule.