LINQ - Query syntax vs method chains & lambda

26,263

To answer your question about translation, the query expression will always be translated based on the rules on 7.16 of the C# 4 spec (or the equivalent in the C# 3 spec). In the example where you're asking the question about the Name property, that's not a matter of the query expression translation - it's what the Select and Where methods do with the delegates or expression trees they take as parameters. Sometimes it makes sense to do a projection before filtering, sometimes not.

As for little rules, I only have one: use whichever way is most readable for the query in question. So if the query changes and "which form is more readable" changes at the same time, change the syntax used.

If you're going to use LINQ you should be happy with either syntax, at the very least to read.

I tend to find that queries with multiple range variable (e.g. via SelectMany or Join, or a let clause) end up being more readable using query expressions - but that's far from a hard and fast rule.

Share:
26,263
Connell
Author by

Connell

I tend to write web applications using C# and JavaScript. I've also had experience in PHP, Objective-C and VBA. I try to follow best practices. I spend a lot of time on object design and pride myself in writing standardised and readable code. The perfectionist type.. Author of Firestorm. Always playing with a few side-projects outside of my day job.

Updated on July 28, 2022

Comments

  • Connell
    Connell almost 2 years

    Does anyone stick to any rules (or are you forced to stick to any rules by your employer?) when choosing to use either LINQ query syntax or a Lambda expression inside one of the LINQ extension methods? This applies to any Entities, SQL, objects, anything.

    At our workplace, my boss doesn't like lambda at all and he'd use the query syntax for anything, which in some cases, I find are less readable.

    var names = collection.Select(item => item.Name);
    
    var names = from item in collection
                select item.Name;
    

    Maybe when adding a condition, the Lambda I find gets a little messy, where the

    var names = collection.Where(item => item.Name == "Fred")
                          .Select(item => item.Name);
    
    var names = from item in collection
                where item.Name == "Fred"
                select item.Name;
    

    Just out of interest: how does the compiler treat this one? Does anyone know how the above LINQ query will compile into lambda? Will the Name property be called for each element? Could we do this instead and potentially improve the performance? Would this mean lambda is slightly more controllable in terms of performance?

    var names = collection.Select(item => item.Name)
                          .Where(name => name == "Fred");
    

    Certainly when we start using more and more expressions, the lambda gets messy and I'd start to use the query syntax here.

    var names = collection.Where(item => item.Name == "Fred")
                          .OrderBy(item => item.Age)
                          .Select(item => item.Name);
    
    var names = from item in collection
                where item.Name == "Fred"
                order by item.Age
                select item.Name;
    

    There are also a few things that I find can't be done with the query syntax. Some of them you'd think would be really simple (particularly aggregate functions), but no, you have to add one of the LINQ extension methods to the end, which imo, look neater with a lambda expression.

    var names = collection.Count(item => item.Name == "Fred");
    
    var names = (from item in collection
                where item.Name == "Fred"
                select item).Count()
    

    Even for some of the simple lambda chains, ReSharper is suggesting I convert them to LINQ querys.

    Can anyone else add to this? Does anyone have their own little rules or does their company suggest/force the use of one?