Differences in syntax support between LINQ and DLINQ

With standard LINQ:

Given:


Func<Customer, bool> IsFromLondon = c => c.City == "London";
Customer[] customers = GetCustomers();

 


We can get customers from London either by calling:


var fromLondon = customers.Where(IsFromLondon);
 


or the simple syntax:


var fromLondon =
  from c in customers
  where IsFromLondon(c)

 



However with DLINQ:

Given:

Expression<Func<Customer, bool>> IsFromLondon = c => c.City == "London";
Table<Customer> customers = GetCustomers();

 


We can get customers from London by calling:

var fromLondon = customers.Where(IsFromLondon);
 


But there is no way of using the simpler syntax

var fromLondon =
  from c in customers
  where // Proposed syntax: c.IsFromLondon or IsFromLondon(c)

 


This is because from DLINQ, Where(...) accepts an expression, not a Func so it cannot be called directly.

And as I proposed in an other post http://forums.microsoft.com/msdn/ShowPost.aspx PostID=89431

Once you get a way to specify expressions within the where statement, it is imperative that you can builder more complex expressions by reusing previous ones:

LINQ:


Func<Cusomer, bool> HasOrders = c => c.Orders.Count() > 0;
var fromLondonWithOrders =
  from c in customers
  where IsFromLondon(c) && HasOrders(c)

 


while with DLINQ:


Expression<Func<Cusomer, bool>> HasOrders = c => c.Orders.Count() > 0;
var fromLondonWithOrders =
  from c in customers
  where // Proposed syntax: IsFromLondon(c) && HasOrders(c) or c.IsFromLondon && c.HasOrders

 


Combining IsFromLondon && HasOrders would create a new BinaryExpression with both parts exactly the same way as we would have typed it explicitly:

c.City == "London" && c.Orders.Count() > 0
 


However the former allows code reuse.

Moreover that code reuse is very important since it mainly represents business logic associated with queries. (Shipped orders, Clients having at least one pending order to be shipped, ...) and it would be really error-prone having to type it over and over again.


Answer this question

Differences in syntax support between LINQ and DLINQ

  • Martin Kelley

    Is this due to a difference in an extension method.  That is, does Table<> have its own version of Where, which doesn't accept Func<>

  • Neelanjana

    Such a functionality was being considered. It did not make it into the preview. Thanks for pointing it out.

  • mjoc69

    Yes, the Table<> version is Where(Expression<Func<T,bool>> pred), so you would have to pass an expression instance instead of a func.



  • Differences in syntax support between LINQ and DLINQ