BreezeJs Navigation Property Count

北战南征 提交于 2019-12-13 02:35:46

问题


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;
    }

回答1:


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()
                };
    }


来源:https://stackoverflow.com/questions/28336823/breezejs-navigation-property-count

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