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?
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.
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.
LINQ provides you three different ways to write a LINQ query in C# or VB.
There is no performance difference between LINQ queries and Lambda expressions.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With