views:

185

answers:

4

It seems there are two ways to build queries -- either using query expressions:

IEnumerable<Customer> result =
                            from customer in customers
                            where customer.FirstName == "Donna"
                            select customer;

or using extension methods:

IEnumerable<Customer> result =
           customers.Where(customer => customer.FirstName == "Donna");

Which do you use and why? Which do you think will be more popular in the long-run?

-Chris

+1  A: 

Only a limited number of operations are available in the expression syntax, for example, Take() or First() are only available using extension methods.

I personally prefer expression if all the required operations are available, if not then i fall back to extension methods as I find them easier to read than lambdas.

take a look at this answer,

Linq Extension methods vs Linq syntax

Keivan
+1  A: 

I use the method syntax (almost) exclusively, because the query syntax has more limitations. For maintainability reasons, I find it preferable to use the method syntax right away, rather than maybe converting it later, or using a mix of both syntaxes.

It might be a little harder to read at first, but once you get used to it, it works fairly natural.

Thorarin
A: 

I only use the method syntax. This is because I find it a lot faster to write, and I write a ton of linq. I also like it because it is more terse. If working on a team, its probably best to come to a concensus as to which is the preferred style, as mixing the two styles is hard to read.

Microsoft recommends the query syntax. "In general, we recommend query syntax because it is usually simpler and more readable". http://msdn.microsoft.com/en-us/library/bb397947.aspx

John Buchanan
A: 

It depends on which you and your team find more readable, and I would choose this on a case by case basis. There are some queries that read better in syntax form and there are some that read better in method form. And of course, there is that broad middle ground where you can't say one way or the other, or some prefer it this way and others that way.

Keep in mind that you can mix both forms together where it might make it more readable.

I see no reason to suspect that either form will dissappear in the future.

Swanny