Re:Best Practices for Linq Enumerables and Queryables
by SimonB86 on Friday August 24, 2012 @12:47PM
(#4428357)
The corresponding extension method syntax is:
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);
Re:Best Practices for Linq Enumerables and Queryables
by libria on Friday August 24, 2012 @01:43PM
(#4428663)
I've used two from's for inner joins:
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.
Re:Best Practices for Linq Enumerables and Queryables
by redstripe on Friday August 24, 2012 @12:00PM
(#4428163)
Assuming you have a foreign key set up between customers and orders, your 'order' entity will automatically have a 'customer' property so you can avoid the 'join' syntax.
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/
E.g. in my code, it would actually be like
vs But often, queries are not that simple and in such scenario query syntax offers far more readability:e.g.
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'.