问题
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