1

I've just started to use BreezeJS and think it is superb. It's saved me so much backend 'plumbing' code which was why I investigated it in the first place. One of the things I would like to be able to do is display a list of some accounts and a count of their contacts. At the moment I am using the code below, the Contacts field is a navigation property in EF6. The issue with this code is that I am downloading all of the contacts for each account (thousands of them) when really all I need is the count. Ideally I'd like my select statement to be something like this '.select('AccountID,AccountName, Contacts.Count()')' but I don't think OData/BreezeJS supports this. Thinking out loud maybe the solution is to create another WebService method called something like AccountsWithContactCounts and return a custom class but Ideally I'd like to avoid this if it is possible to do in the breezejs client.

Welcome any thoughts, apologies if this is a basic question.

breeze.EntityQuery.from('Accounts')
        .where(pred)
        .select('AccountID,AccountName, Contacts')
        .orderBy(orderbyClause(orderBy, orderByReverse))
        .skip(currentPage * pageItems)
        .take(pageItems)
        .noTracking()
        .using(breezemanager).execute()
        .then(function (response) {
            console.log(response.results.length + ' entities retrieved');
            $scope.items = response.results;
        })
        .catch(function (error) {
            console.log(error.message);
        })
        .finally(function () {
        });

And my breeze service looks like this:

  [HttpGet]
    public IQueryable<Account> Accounts()
    {
        return _contextProvider.Context.Accounts;
    }
Jim Culverwell
  • 2,623
  • 6
  • 25
  • 28
  • You answered your own question: create a custom method on the server that does it for you. Your method can return anonymous objects if you don't want to create a new class for this. – Steve Schmitt Feb 05 '15 at 07:49

1 Answers1

2

As Steve confirmed the answer was to create a new method on the Breeze controller

 [HttpGet]
    public IQueryable<object> AccountsSummary()
    {
        return from t in _contextProvider.Context.Accounts
            select
                new
                {
                    t.AccountID,
                    t.AccountName,
                    ContactsCount = t.Contacts.Count()
                };
    }
Jim Culverwell
  • 2,623
  • 6
  • 25
  • 28
  • 1
    By the way, you can return an entity inside your anonymous object (for example `select new { t, ContactsCount = t.Contacts.Count };` ) and breeze will handle it correctly on the client (i.e. add the entity to the cache, hook it up to any related entities in the cache). – Steve Schmitt Feb 06 '15 at 23:18