My last post (which was too long ago), generated question on whether I prefer the query language or the method notation for LINQ queries.
The answer is ‘yes’. Jon Skeet mentioned this as well last January, but anything you can do with query syntax can be accomplished with method calls. (Item 36 of More Effective C# discusses is some detail how the query language operators map to method calls.)
From the standpoint of correctness, you can use either construct with no differences. That means it is a matter of style whether you choose the query expressions or the method calls.
My own preference is to use the method call syntax for simpler queries, and use the query syntax for more complex query operations.
For example, I would use the method call syntax below over the query syntax:
However, once a query gets sufficiently complex, I believe the query syntax is more readable:
In my opinion, for more complex queries, the query language produces a much more readable construct. However, on simpler queries, the method calls are easier to understand.
I like Jon’s comment: “next time you’re writing a query expression, take a look at it afterwards – it it’s simple, try writing it without the extra syntactic sugar.” (from the post referenced above).
All of these projects are Open Source (using the Creative Commons license for content, and the MIT license for code). If you would like to contribute, visit our GitHub Repository. Or, if you have questions, comments, or ideas for improvement, please create an issue for us.