Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ - Query syntax vs method chains & lambda [closed]

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?

like image 887
Connell Avatar asked Nov 07 '11 14:11

Connell


People also ask

What is the difference between query syntax and method syntax in LINQ?

Query syntax and method syntax are semantically identical, but many people find query syntax simpler and easier to read. Some queries must be expressed as method calls. For example, you must use a method call to express a query that retrieves the number of elements that match a specified condition.

What is LINQ syntax?

LINQ query syntax is consist of a set of query keywords defined into the . NET Framework version 3.5 or Higher. This allows the programmer or developers to write the commands similar to SQL style in the code(C# or VB.NET) without using quotes. It is also know as the Query Expression Syntax.

How many types of LINQ query are there?

LINQ provides you three different ways to write a LINQ query in C# or VB.

Which is faster LINQ or Lambda?

There is no performance difference between LINQ queries and Lambda expressions.


1 Answers

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.

like image 129
Jon Skeet Avatar answered Oct 26 '22 18:10

Jon Skeet