Query Language or Method calls: A matter of taste

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:

   1: int[] numbers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
   2: var smallNumbers = from n in numbers
   3:                    where n < 5
   4:                    select n;
   5:  
   6: var smallNumbers2 = numbers.Where((n) => n < 5);

However, once a query gets sufficiently complex, I believe the query syntax is more readable:

   1: int[] odds = { 1, 3, 5, 7 };
   2: int[] evens = { 2, 4, 6, 8 };
   3: var values = from oddNumber in odds
   4:             from evenNumber in evens
   5:             where oddNumber > evenNumber
   6:             select new { oddNumber, evenNumber, 
   7:             Sum = oddNumber + evenNumber };
   8:  
   9: var values2 = odds.SelectMany(oddNumber => evens,
  10:     (oddNumber, evenNumber) =>
  11:     new { oddNumber, evenNumber })
  12:     .Where(pair => pair.oddNumber > pair.evenNumber).
  13:     Select(pair => new { 
  14:         pair.oddNumber, 
  15:         pair.evenNumber, 
  16:         Sum = pair.oddNumber + pair.evenNumber });

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).

Published 11 May 2009 01:54 PM by wwagner
Ads by Lake Quincy Media

Comments

# Joe Gilray said on 12 May, 2009 10:43 PM

Hi Bill,

Thanks for giving me a chance to (over)state my case!

I originally asked, "is there a reason to learn query format?"  I guess here, I will try this statement out, "Isn't it better to learn method notation well, before wasting time with query format?"

You say "use the right tool for the job" which sounds so reasonable... I say, "query format is icky".

First, the verbs in the query format are not C# keywords, I've even seen code where variables were given names like group... besides query format is ugly!

Second, with method notation you get to use intellisense to help with method names and argument types, this is a big win for me.

Third, the query format looks like SQL but works differently... avoiding query format may help avoid confusion.

Fourth, did I mention how ugly it is?

Fifth, I know you can mix the formats, but if you learn one format well, you're less like to make mistakes or get confused.  For a new learner I think that is important.

Last, your example of the method notation can be simplified to:

var values2 = odds.SelectMany(odd => evens.Where(even => odd>even), (odd,even) => new {odd, even, Sum=odd + even});

Which may not quite scan as easily as your query format example, but isn't too bad.

There, Now I feel better!  Your turn.

-Joe

# 宗子城 said on 14 May, 2009 09:27 PM

very nice C# tips about Query Language or Method calls!

Search

Go

Blog Group Links

Nascar style badges