Mark Needham

Thoughts on Software Development

Functional C#: LINQ vs Method chaining

with 6 comments

One of the common discussions that I’ve had with several colleagues when we’re making use of some of the higher order functions that can be applied on collections is whether to use the LINQ style syntax or to chain the different methods together.

I tend to prefer the latter approach although when asked the question after my talk at Developer Developer Developer I didn’t really have a good answer other than to suggest that it seemed to just be a personal preference thing.

Damian Marshall suggested that he preferred the method chaining approach because it more clearly describes the idea of passing a collection through a pipeline where we can apply different operations to that collection.

I quite like that explanation and I think my preference for it would have probably been influenced by the fact that when coding in F# we can use the forward piping operator to achieve code which reads like this.

For example if we had a list and wanted to get all the even numbers, double them and then add them up we might do this:

[1..10] |>
List.filter (fun x -> x % 2 = 0) |>
List.map (fun x -> x * 2) |>
List.fold (fun acc x -> acc + x) 0

If I was in C# I’d probably do this:

Enumerable.Range(1, 10)
.Where(x => x % 2 == 0)
.Select(x => x * 2)
.Sum(x => x);

I found it quite difficult to work out what the equivalent LINQ syntax would be because I don’t use it but I think something like this would be what you’d need to write to do the same thing:

from x in Enumerable.Range(1, 10)
where x%2 == 0
select x * 2).Sum(x => x);

I’m not sure if there’s a way to do the sum within the LINQ statement or whether you need to do it using the method as I have here.

Even just writing this example I found that the way I had to write the LINQ code seemed quite counter intuitive for me with the way that I typically try to solve problems like this.

At least now thanks to Damian I now understand why that is!

Written by Mark Needham

February 5th, 2010 at 6:06 pm

Posted in .NET

Tagged with

  • http://olabini.com/blog Ola Bini

    I think you can use aggregate in the Linq statement to sum it – but I’m not a 100% about that.

  • http://www.silverlighthack.com Bart Czernicki

    I prefer LINQ’s terse and declareative nature.

    I think method chaining is valuable in situations where you need to build your queries dynamically.

    It would be interesting to see the performance difference between the three options. My guess would be that F#’s syntax/compiler would be faster and LINQ would take the most memory.

  • http://www.markhneedham.com Mark Needham

    I don’t know for definite but I was of the opinion that the LINQ syntax compiles down to be the method calls anyway?

  • googly

    @Mark: That is my understanding as well.
    Also, in ReSharper, there’s a refactoring command that can convert between the two different statement types.

  • Matt Warren

    There’s a good discussion on StackOverflow that talks about this, see http://stackoverflow.com/questions/214500/.

    Also from the msdn page http://msdn.microsoft.com/en-us/library/bb397947.aspx:

    In general, we recommend query syntax because it is usually simpler and more readable; however 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.

    So there is no difference between the 2 by the time it’s executed.

  • Bent Rasmussen

    LINQ syntax is just syntactic sugar for direct method chaining so it depends on what syntax you prefer. I tend to favor method chaining in general.