LINQ - Query syntax against method chains and lambda

Does anyone adhere to any rules (or do you have to adhere to any rules by your employer?) When choosing to use the LINQ query syntax or the Lambda expression inside one of the LINQ extension methods? This applies to any objects, SQL, objects, etc.

At our workplace, my boss doesn't like lambda at all, and he uses the query syntax for something that in some cases I find less readable.

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

Perhaps when adding a condition, the Lambda that I find is a bit confusing where

 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 relate to this? Does anyone know how this LINQ query will compile to lambda? Will the Name property be called for each element? Can we do this instead and potentially improve performance? Would this mean that lambda is a bit more controllable in terms of performance?

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

Of course, as we start using more and more expressions, the lambda becomes messy, and I would start using 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 cannot be performed with the query syntax. Some of them, in your opinion, will be very simple (in particular, aggregate functions), but no, you need to add one of the LINQ extension methods to the end, which imo looks more neat with the 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 simple lambda chains, ReSharper offers to convert them to LINQ queries.

Can anyone else add to this? Does anyone have their own rules or does their company offer / enforce one?

+47
c # lambda linq linq-query-syntax
Nov 07 2018-11-11T00:
source share
1 answer

To answer the question of translation, the query expression will always be translated based on the rules of 7.16 of the C # 4 specification (or the equivalent in the C # 3 specification). In the example, when you ask a question about the Name property, it’s not a question of translating the query expression β€” this is what the Select and Where methods do with the delegates or expression trees that they accept as parameters. Sometimes it makes sense to do a projection before filtering, sometimes not.

As for the small rules, I have only one: use any method that is most readable for the issue in question. Therefore, if the request changes and β€œwhich form is more readable” changes at the same time, change the syntax used.

If you intend to use LINQ, you should be happy with the syntax, at least for reading.

I usually find that queries with multiple range variables (e.g. via SelectMany or Join or a let clause) become more readable using query expressions, but this is far from a hard and fast rule.

+27
Nov 07 2018-11-11T00:
source share



All Articles