c# - LINQ - Query syntax vs method chains & lambda -
does stick rules (or forced stick rules employer?) when choosing use either linq query syntax or lambda expression inside 1 of linq extension methods? applies entities, sql, objects, anything.
at our workplace, boss doesn't lambda @ , he'd use query syntax anything, in cases, find less readable.
var names = collection.select(item => item.name); var names = item in collection select item.name;
maybe when adding condition, lambda find gets little messy,
var names = collection.where(item => item.name == "fred") .select(item => item.name); var names = item in collection item.name == "fred" select item.name;
just out of interest: how compiler treat one? know how above linq query compile lambda? name
property called each element? instead , potentially improve performance? mean lambda more controllable in terms of performance?
var names = collection.select(item => item.name) .where(name => name == "fred");
certainly when start using more , more expressions, lambda gets messy , i'd start use query syntax here.
var names = collection.where(item => item.name == "fred") .orderby(item => item.age) .select(item => item.name); var names = item in collection item.name == "fred" order item.age select item.name;
there few things find can't done query syntax. of them you'd think simple (particularly aggregate functions), no, have add 1 of linq extension methods end, imo, neater lambda expression.
var names = collection.count(item => item.name == "fred"); var names = (from item in collection item.name == "fred" select item).count()
even of simple lambda chains, resharper suggesting convert them linq querys.
can else add this? have own little rules or company suggest/force use of one?
to answer question translation, query expression translated based on rules on 7.16 of c# 4 spec (or equivalent in c# 3 spec). in example you're asking question name
property, that's not matter of query expression translation - it's select
, where
methods delegates or expression trees take parameters. makes sense projection before filtering, not.
as little rules, have one: use whichever way readable query in question. if query changes , "which form more readable" changes @ same time, change syntax used.
if you're going use linq should happy either syntax, @ least read.
i tend find queries multiple range variable (e.g. via selectmany
or join
, or let
clause) end being more readable using query expressions - that's far hard , fast rule.
Comments
Post a Comment