|
|
|
|
|
by gregmac
3414 days ago
|
|
Our codebase at work has hundreds if not thousands of LINQ queries, mostly using method syntax. I generally prefer it over query syntax except in a few cases, such when there's multiple joins or subqueries, or the when the statement is more than a few dozen lines. Occasionally a very complex LINQ statement requires using a `let` declaration which also is easier with query syntax. Luckily Resharper has an action to convert back and forth on the fly, so it's easy to use that to switch over when necessary. I also don't use LINQ to SQL at all. It's all on in-memory stuff. I pretty much never write code like this: var result = new List<string>();
foreach (var item in input)
{
result.Add(item.Value);
}
return result;
instead, using LINQ: return items.Select(x => x.Value);
The real power comes when you start mixing conditions: return items
.Where(x => IsValidKey(x.Key))
.Select(x => x.Value);
Or doing quick checks: if (items.Any(x => x == null || x.SomeValue == null))
throw new InvalidArgumentException(nameof(items));
|
|
if(items.Any(x => x?.SomeValue == null))