I am using Breezejs for client JavaScript. I am not sure how to get the Total count of query when using Breezejs (server side using IQueryable) that has filters (where clause) applied.
As of v 0.75.1 we have added a new 'inlineCount' method to the EntityQuery. Please see the breeze API docs for more details. Hopefully, this will provide what you need.
We made a little change to Ward's solution, because Enumerable.Count loads all records from db, not only count (we realized this after watching sql profiler).
First we create a wrapper for IQueryable.Count extension method (because we cannot call an extension method via reflection).
public static class QueryWrapper {
public static int Count<T>(IQueryable<T> query) where T: class {
return query.Count();
}
}
and we changed
if (actionExecutedContext.Request.RequestUri.Query.Contains("$inlinecount")) {
if (dQuery is IQueryable) {
inlineCount = Enumerable.Count(dQuery);
}
}
with this code,
if (actionExecutedContext.Request.RequestUri.Query.Contains("$inlinecount")) {
if (dQuery is IQueryable) {
var method = typeof(QueryWrapper).GetMethod("Count");
var genericMethod = method.MakeGenericMethod(elementType);
inlineCount = (int)genericMethod.Invoke(null, new object[] { dQuery });
}
}
after this change, Entity Framework gets only count, not all records.
Hope this helps.
Have a nice day.
Support $inlinecount in queries currently is under review - please vote for it.
UPDATE: $inlineCount is in Breeze as of v.0.75.1, rendering this answer obsolete.
Accordingly, I have deleted my answer which described a workaround (a workaround improved by Umut Özel in his answer here). The new feature was driven by this S.O. question and all of your contributions. Thank you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With