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:

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

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

          int[] odds = { 1, 3, 5, 7 };
          int[] evens = { 2, 4, 6, 8 };
             3: var values = from oddNumber in odds
             4:             from evenNumber in evens
          where oddNumber > evenNumber
             6:             select new { oddNumber, evenNumber, 
             7:             Sum = oddNumber + evenNumber };
             9: var values2 = odds.SelectMany(oddNumber => evens,
            10:     (oddNumber, evenNumber) =>
          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).

Created: 5/11/2009 5:54:09 PM

Current Projects

I create content for .NET Core. My work appears in the .NET Core documentation site. I'm primarily responsible for the section that will help you learn C#.

All of these projects are Open Source (using the Creative Commons license for content, and the MIT license for code). If you would like to contribute, visit our GitHub Repository. Or, if you have questions, comments, or ideas for improvement, please create an issue for us.

I'm also the president of Humanitarian Toolbox. We build Open Source software that supports Humanitarian Disaster Relief efforts. We'd appreciate any help you can give to our projects. Look at our GitHub home page to see a list of our current projects. See what interests you, and dive in.

Or, if you have a group of volunteers, talk to us about hosting a codeathon event.