WAQS Querying: support entities extension methods on LINQ queries

7 reasons to use WAQS

WAQS documentation

 

We saw that LINQ To WAQS (L2WAQS) and LINQ To Entities + WAQS (L2EW) support calculated properties.

L2WAQS and L2EW also support specification service / sub entities extension methods.

So for example, imagine that you use the following specifications:

public static double GetAmount(this OrderDetail od)
{
     return od.Quantity * od.UnitPrice * (1 - od.Discount); } public static double GetTotal(this Order o) {
     return o.OrderDetails.Sum(od => od.GetAmount()); } public static double GetTotalSpentFrom(this Customer c, DateTime d) {
     return c.Orders.Where(o => o.OrderDate >= d).Sum(o => o.GetTotal()); }


You can use this L2WAQS query:



var minDate = new DateTime(DateTime.Now.Year, 1, 1);
var goodCurrentYearCustomers = await _context.Customers.AsAsyncQueryable().Where(c => c.GetTotalSpentFrom(minDate) > 1000).ExecuteAsync();


And in the server, you can use the equivalent for L2EW:



var minDate = new DateTime(DateTime.Now.Year, 1, 1);
var goodCurrentYearCustomers = context.Customers.Where(c => c.GetTotalSpentFrom(minDate) > 1000);
This entry was posted in 12253, 16868. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>