score:6

Accepted answer

there is no semantic difference between method syntax and query syntax. in addition, some queries, such as those that retrieve the number of elements that match a specified condition, or that retrieve the element that has the maximum value in a source sequence, can only be expressed as method calls.

http://msdn.microsoft.com/en-us/library/bb397947.aspx

also look here: .net linq query syntax vs method chain

it comes down to what you are comfortable with and what you find is more readable.

score:1

the second one use lambda expressions. i like it as it is compact and easier to read (although some will find the former easier to read).

also, the first is better suited if you have a sql background.

score:1

i'd say go with what is most readable or understandable with regards to your development team. come back in a year or so and see if you can remember that linq... well, this particular linq is obviously simple so that's moot :-)

best practice is also quite opinionated, you aren't going to get one answer here. in this case, i'd go for the second item because it's concise and i can personally read and understand it faster than the first, though only slightly faster.

score:1

i personally much prefer using lambda expressions. as far as i know there is no real difference as you say you can do exactly the same thing both ways. we agreed to all use the lambda as it is easy to read, follow and to pick up for people who don't like sql.

score:1

there is absolutely no difference in terms of the results, assuming you do actually write equivalent statements in each format.

go for the most readable one for any given query. complex queries with joins and many where clauses etc are often easier to write/read in the linq query syntax, but really simple ones like context.employees.singleordefault(e => e.id == empid) are easier using the method-chaining syntax. there's no general "one is better" rule, and two people may have a difference of opinion for any given example.

score:1

there is no semantic difference between the two statements. which you choose is purely a matter of style preference

score:1

do you need the explicit cast in either of them? isn't time already a datetime?

personally i prefer the second approach as i find the extension method syntax more familiar than the linq syntax, but it is really just personal preference, they perform the same.

the second one written to more exactly look like the first would be context.datas.select(x => x.time).min(). so you can see how you wrote it with min(x => x.time) might be slightly more efficient, because you only have on operation instead of two

score:1

the query comprehension syntax is actually compiled down to a series of calls to the extension methods, which means that the two syntaxes are semantically identical. whichever style you prefer is the one you should use.


Related Query