1

I can do:

var something = things.Where(thing => thing.stuff == yup);
var somethingelse = something.Select(thing => thing.otherstuff);

or

var something = from thing in things
                where thing.stuff == yup
                select thing;
var somethingelse = from thing in something
                    select thing.otherstuff;

Obviously if this were real world there's the benefit with the keyword version of doing:

var somethingelse = from thing in something
                    where thing.stuff == yup
                    select thing.otherstuff;

But then of course you could argue that you could do:

var somethingelse = things.Where(thing => thing.stuff == yup)
                          .Select(thing => thing.otherstuff);

Anyway the question itself: what are the pros/cons to using each of these variants? Are they identical but just different syntax code-side? If you combine two method versions (i.e., where/select as above) is it less efficient than using the keyword syntax combining both into one line?

I love LINQ and I don't want to lose any efficiency where some could be gained by using one type or another.

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
Yushatak
  • 741
  • 1
  • 5
  • 15
  • the sql based syntax of linq gets converted to the method calls by the compiler. no performance difference in the end. – Mike Corcoran May 10 '13 at 17:16
  • 4
    If I remember correctly, the LINQ syntax always gets compiled down to the method syntax, so there should not be any efficiency lost. – Porkbutts May 10 '13 at 17:16
  • That's what I thought as well, but I wanted to be sure. Thanks guys! :D – Yushatak May 10 '13 at 17:18
  • It's all about readability. Sometimes I end up writing it both ways and then keeping the one that is more understandable. – Euro Micelli May 10 '13 at 17:19

5 Answers5

3

The query syntax will be converted into the method syntax by the compiler in a first pass, and then compiled from that method syntax into IL code in a second pass. There is no difference in the resulting compiled code between code written using the query syntax verses code written directly in method syntax. (Although not all of the methods are represented in query syntax, so some queries cannot be written without partial or complete use of method syntax.)

The only difference is the personal preference of the coder; what you find easier to read or write.

From my personal experiences certain types of queries are easier to read and/or write in one syntax over the other, but that's entirely a matter of opinion and does vary between people. Use whichever you're most comfortable with in any given situation.

Servy
  • 202,030
  • 26
  • 332
  • 449
0

According to MSDN some queries must be written in the method syntax, but in general they are identical. Query syntax is converted to method syntax before compilation so they end up generating the same IL. I personally prefer method syntax. I think consistency is important and so I use only method syntax.

via msdn;

"For example, you must use a method call to express a query that retrieves the number of elements that match a specified condition."

Article with all the info you're looking for; http://msdn.microsoft.com/en-us/library/vstudio/bb397947.aspx

evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
  • 1
    I disagree that you should use exclusively one or the other. If a query happens to just fit much more effectively into the query syntax model, it's often helpful to use that, likewise if a particularly query can be more logically understood broken up into it's components through method syntax it can be much easier to understand. Saying one should be globally discarded doesn't seem appropriate. (And of course you *can't* discard method syntax, as not all operations exist in query syntax, so what you're effectively saying is that you shouldn't use query syntax.) – Servy May 10 '13 at 17:23
  • 1
    @Servy I didn't say everyone should, I just said I do. But I don't really agree with that. Query syntax offers a subset of the queries method syntax has. I understand people doing things differently, like if you're coming from a SQL background you'll probably find Query Syntax more intuitive, however I'm coming from a CS background and had to take several lambda calculus classes so the method syntax imo is strictly more readable. Plus it's more powerful. Why waste time with two things when I can just be better at the better of the two? – evanmcdonnal May 10 '13 at 17:29
0

Compiler will treat the code in both method and LINQ syntax the same way. Use may depend on your own ability and preference.

This SO thread has some answers on this issue.

This SO answer warns that this all depend on implementation for LINQ.

Community
  • 1
  • 1
kiriloff
  • 25,609
  • 37
  • 148
  • 229
0

The LINQ query operators you call out here function as syntactic sugar for the extension methods in the System.Linq namesapce*, and as such, are identical as far as runtime efficiency. You can even create your own query opertors that the compiler will magically translate into methods, if you so choose.

Part 3 of the excellent C# in Depth by Jon Skeet covers LINQ, including this question, in much more detail if you're interested in learning more. See also http://msdn.microsoft.com/en-us/library/vstudio/bb397947.aspx and http://msdn.microsoft.com/en-us/library/vstudio/bb397896.aspx on MSDN.

*You can also provide your own implementations of the extension methods normally exposed in the System.Linq namespace, in the same way you can create custom LINQ operators.

Community
  • 1
  • 1
Preston Guillot
  • 6,493
  • 3
  • 30
  • 40
  • 1
    The query syntax is actually not tied to the `System.Linq` namespace in any way. [It can be used with any methods that have the appropriate signature](http://blogs.msdn.com/b/ericlippert/archive/2009/12/07/query-transformations-are-syntactic.aspx). – Servy May 10 '13 at 17:30
  • @Servy You're correct, I was making the assumption that he was using the implementations in the `System.Linq` namespace given the nature of the question, but those operators aren't bound to those implementations in any way. – Preston Guillot May 10 '13 at 17:56
  • I think it's pretty clear that the OP is using the LINQ operations in his examples. My point was that, while the OP is using it here, it doesn't mean that every use of query syntax ever is using LINQ. Most are, in fact almost all are, but it's at least possible to not. – Servy May 10 '13 at 17:59
  • Edited for cromulence. – Preston Guillot May 11 '13 at 01:12
0

When using the query syntax (using the LINQ keywords), the compiler translates the code into the corresponding LINQ method calls yielding equivalent code. As far as the query is concerned, there is no difference in performance, each should produce the same results. It only affects readability of the query depending on which you are more comfortable with.

The only "problems" (if you see it that way) with the query syntax is that it will translate to a specific set of LINQ methods. If you look at all the LINQ methods available, there are additional overloads available for many of the standard operators as well as other methods not available in the query syntax. You will not be able to use these methods in such a query.

For instance, you cannot use the Distinct() operator on queries using the query syntax (as of now, hopefully in a future version and backported). If you wanted to use it, you must use the method syntax. So you would have to either mix syntaxes (which is a little ugly when queries get more complex IMHO) or use the method calls exclusively.

// get all distinct user names
var query1 =
    (from user in Users
    select user.Name).Distinct();

// vs.
var query2 = Users
    .Select(user => user.Name)
    .Distinct();

If you asked me, use the query syntax whenever possible. Use the method call syntax if absolutely necessary. Mixing styles is a judgement call, just go for whatever is more readable and be consistent.

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
  • 1
    `One thing the query syntax will not allow you to do is write dynamic queries.` This is just not true. You could replace the body of the `if` with: `query = from user in query where user.Age >= age select age`. The result will be an identical query. – Servy May 10 '13 at 17:37
  • I suppose that wasn't the best choice of words but seriously, _would_ you want to write it that way? (I hope not) If you're going to break up a query across multiple statements, the query syntax does not lend itself well for that usage. It will only lead to uglier code (IMHO). If you're going to mix in regular code, you might as well stick to the method calls. But as I said, a judgement call. – Jeff Mercado May 10 '13 at 17:44
  • 1
    You stated that it's not possible to break up a query using query syntax into multiple calls. *This is provably false*. If you want to state that you feel it is less readable, more verbose, or otherwise unintuative *for you* to break up queries using query syntax then that can at least be a valid statement. Personally I've seen it done on a number of occasions in which I felt that, in context, it was perfectly appropriate to do. – Servy May 10 '13 at 17:48
  • To each his own I guess. I personally avoid using the query syntax when composing a long query over multiple statements (such as those created in dynamic queries). It would be a different matter if the subqueries can stand alone. – Jeff Mercado May 10 '13 at 17:53