Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get TotalRows Count in Breezejs, so that I can do paging

Tags:

breeze

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.

like image 749
NSS Avatar asked Dec 01 '12 20:12

NSS


4 Answers

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.

like image 129
Jay Traband Avatar answered Nov 13 '22 11:11

Jay Traband


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.

like image 44
Umut Ozel Avatar answered Nov 13 '22 09:11

Umut Ozel


Support $inlinecount in queries currently is under review - please vote for it.

like image 37
pawel Avatar answered Nov 13 '22 09:11

pawel


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.

like image 42
Ward Avatar answered Nov 13 '22 11:11

Ward