Breeze filtering .Expand on server side

只愿长相守 提交于 2019-12-01 23:16:09

Not entirely sure I understand your issue, but you can perform the 'expand' on the server side via an EF 'Include' like that shown below:

 [HttpGet]
 public IQueryable<Customer> CustomersAndOrders() {
   var custs = ContextProvider.Context.Customers.Include("Orders");
   return custs;
 }

Which will return 'Customer' objects each with its 'Orders' property fully populated and loading into the Breeze cache.

If you want to actually suppress 'expansion' on the server for a given resource name you can use the the [BreezeQueryableAttribute]. Note that AllowedQueryOptions.Expand is omitted from the list of supported operations in the example below.

[HttpGet]
[BreezeQueryable(AllowedQueryOptions = AllowedQueryOptions.Filter | AllowedQueryOptions.Skip | AllowedQueryOptions.Top | AllowedQueryOptions.OrderBy)]
public IQueryable<Employee> Employees() {
  return ContextProvider.Context.Employees;
}

The [BreezeQueryableAttribute] supports the same parameters as Microsoft's [QueryableAttribute] described here: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options

The other option if you want to actually restrict/filter what gets expanded can only be done by performing the filtering expansion yourself, possibly with the help of parameters passed into the method via 'withParameters' (This is because EF does not yet support filtering on 'Includes'. I have not tested the example below but the general idea should work.

[HttpGet]
public IQueryable<Employee> Employees(double minWeight) {
  var emps = ContextProvider.Context.Employees.Include("Orders").ToList();
  // remove selected orders from what gets returned to the client.
  emps.ForEach(emp => {
    var ordersToRemove = emp.Orders.Where(o => o.Freight < minWeight).ToList();
    ordersToRemove.ForEach(o => emp.Orders.Remove(o));
  });
  return emps;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!