tags:

views:

107

answers:

3

Of these two options:

var result = from c in coll where c % 2 == 0 select c;

var result = coll.Where ( c => c % 2 == 0 );

Which is preferable?

Is there any advantage to using one over the other? To me the second one looks better, but I would like to hear other people's opinions.

+7  A: 

If you've only got one or two clauses, I'd go for "dot notation". When you start doing joins, groupings, or anything else that introduces transparent identifiers, query syntax starts to appeal a lot more.

It's often worth trying it both ways and seeing what's the most readable for that particular situation.

In terms of the generated code, they'll be exactly the same in most cases. Occasionally there'll be an overload you can use in dot notation which makes it simpler than the query expression syntax, but value readability over everything else in most cases.

I also have a blog post on this topic. I would definitely recommend that developers should be comfortable with both options - I'd be quite concerned if a colleague were using LINQ but didn't understand the fundamentals of what query expressions were about, for example. (They don't need to know every translation involved, but some idea of what's going on will make their lives a lot easier.)

Jon Skeet
Thanks Jon. Corrected it now. Btw what do you mean by "transparent identifiers"?
Joan Venge
(I'll edit the answer to remove that bit.) Transparent identifiers are introduced so that if you've got multiple range variables involved (e.g. from x in Foo from y in x.Something - x and y are range variables) it all still works, even though you've only got a single sequence later on - basically the compiler creates more anonymous types for you. Doing that manually can make the code less clear.
Jon Skeet
Thanks Jon. Can one use intermediate values using "let" in dot notation? Also how would your example "from x in Foo from y in x.Something" would look in dot notation? Foo.Where(...).Where?
Joan Venge
@Joan, FYI the answers to all questions about precisely how query comprehensions are translated into method calls is in the C# 3.0 spec, which you can get off the internet.
Eric Lippert
Indeed, at http://download.microsoft.com/download/3/8/8/388e7205-bc10-4226-b2a8-75351c669b09/CSharp%20Language%20Specification.doc
Jon Skeet
@Eric: I don't think average users want to open the spec to find out this kind of information. I think most people assume that a language spec is usually scary and difficult to use as a reference...which is often true. I'm sure you have a totally different perspective on this, though. As do people like Jon, who are writing easier-to-read references based at least in part on the spec.
Brian
+2  A: 

I always use the lambda syntax because to me it's clearer what's actually happening and it looks cool to boot. But we have some devs here that always do the opposite (sql nerds I guess :) Fortunately, tools like ReSharper can just transform between the two with a click.

JP Alioto
+1  A: 

Check this question:

CMS