3 ms·
About your first point, sometimes using method syntax is more readable than query. When you are not reliying heavily on LINQ and just use some commands, is easi
by dmiranda 14y ago
About your first point, sometimes using method syntax is more readable than query. When you are not reliying heavily on LINQ and just use some commands, is easier to write
var males = customers.Where(c => c.Gender == "male");
than
var males = from c in customers where c == "male" select c;
Not only because it's longer, but also because it can feel strange if you're not using it continously.
- solutionyogi 14y agoReadability is definitely subjective. For simple scenarios, I do prefer the extension method. 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'.
- deleted 14y ago[deleted]
- redstripe 14y agoAssuming 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/ http://blogs.teamb.com/craigstuntz/2010/01/13/38525/
- SimonB86 14y agoThe 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);
- libria 14y agoI'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.