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

4

6

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.

Lactose answered 1/12, 2012 at 20:47 Comment(1)
@NS - If you think the new "inlineCount" feature in Breeze satisfies your question, please click the checkmark under Jay's answer in which he announces that "inlineCount" is part of Breeze.Fides
L
5

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.

Lambeth answered 7/12, 2012 at 20:53 Comment(1)
Woo-hoo! breezejs.com/sites/all/apidocs/classes/…Stellular
D
2

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.

Devisal answered 6/12, 2012 at 14:41 Comment(2)
Thanks for the correction! I must go back and add that to my hack. Better yet, we should get it into the product :)Fides
Now available as part of breeze with v 0.75.1. In the source you might note the change from Enumerable.Count(dQuery) -> Queryable.Count(dQuery), which accomplishes the same thing.Lambeth
A
1

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

Akerboom answered 1/12, 2012 at 23:20 Comment(2)
Your voice was heard and we're currently working on it. This should be available shortly (days).Upwards
Your voice was heard and it is in Breeze as of v 0.75.1Fides
F
1

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.

Fides answered 2/12, 2012 at 19:48 Comment(3)
Hi Ward, I have added our little change as an answer. Thanks for this hack btw, it works like a charm.Procrustes
Ward why did you deleted the workaround it is always nice to have the solutionsLactose
I deleted it because I promised to delete it once Breeze added the inlineCount feature. Why confuse people with a workaround? Btw, my workaround didn't conform precisely to the OData spec.Fides

© 2022 - 2024 — McMap. All rights reserved.