Counts in Breeze.js

倾然丶 夕夏残阳落幕 提交于 2019-12-07 06:43:03

问题


I have a complex query that returns item counts. If I run a query on the client, will it always return the objects, or is there a way to just return the item counts without sending the object array in the payload? I tried doing something like

var query = breeze.EntityQuery.from('Items').inlineCount(true);

but that still pulls all the records down. Any solutions?


回答1:


I don't know if this exactly answers your question, but you would need to query the records in order to know how many there are (to my knowledge, there may be a more efficient way to tie Breeze directly into a SQL command that is way over my head) so you could do something like -

var query = breeze.EntityQuery.from('Items')
    .take(0)
    .inlineCount(true);

Edited the answer - this would return no objects and simply get the count.




回答2:


The inlineCount answer already provided is absolutely correct.

Another alternative is to calculate the counts on the server and just send down the "summary". For example this server side controller method will return an array of two element objects to the client:

  [HttpGet]
  public Object CustomerCountsByCountry() {
    return ContextProvider.Context.Customers.GroupBy(c => c.Country).Select(g => new {g.Key, Count = g.Count()});
  }

This would be called via

  EntityQuery.from("CustomerCountsByCountry")
     .using(myEntityManager).execute()
     .then(function(data) {

     var results = data.results;
     results.forEach(function(r) {
       var country = r.Key;
       var count = r.Count
     });

  });



回答3:


var query = breeze.EntityQuery.from('Items') .take(0) .inlineCount(true);



来源:https://stackoverflow.com/questions/16390897/counts-in-breeze-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!