by solutionyogi · 14y ago · 0 comments
E.g. in my code, it would actually be like
var males = customers.Where(c => c.IsMale);
vs var males = from c in customers where c.IsMale select c;
But often, queries are not that simple and in such scenario query syntax offers far more readability:e.g.
var filteredCustomers =
from c in customers
join o in orders on o.customerid = c.customerid
where c.IsMale && c.Age > 30
where o.IsPending
select new {Customer = c, Order = o};
The corresponding extension method syntax will not be readable. In fact, I don't even know how to write that code off top of my head.My point was that the whole purpose of query syntax was to improve readability and it was not about 'pretty' or 'clever'.
var filteredCustomers = customers
.Join(orders,
c => c.customerid,
o => o.customerid,
(c, o) => new { Customer = c, Order = o }
)
.Where(x => x.Customer.IsMale && x.Customer.Age > 30)
.Where(x => x.Order.IsPending); var filteredCustomers =
from c in customers
from o in orders
where o.customerid = c.customerid &&
c.IsMale && c.Age > 30 &&
o.IsPending
select new {Customer = c, Order = o};
This is one of those cases where I'll favor query syntax over extensions. OTOH, query syntax requires an explicit `select` where it's often optional in extensions. Also, `.ToList()` doesn't have a query syntax counterpart so you'll often mix both if you tend to favor query.If your objective is to find orders that belong to male customers over 30:
filteredCustomers = orders.Where(o => o.IsPending && o.customer.IsMale && o.customer.Age > 30);
Some more about avoiding joins (which makes it easier to use the extension syntax): http://blogs.teamb.com/craigstuntz/2010/01/13/38525/