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?