BreezeJs导航属性计数 [英] BreezeJs Navigation Property Count

查看:50
本文介绍了BreezeJs导航属性计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用BreezeJS,并认为它很棒。它为我节省了很多后端管道代码,这就是为什么我首先对其进行调查的原因。我想做的一件事是显示一些帐户的列表及其联系方式。目前,我正在使用以下代码,联系人字段是EF6中的导航属性。这段代码的问题是,当我真正需要的只是计数时,我正在下载每个帐户(数千个)的所有联系人。理想情况下,我希望我的select语句是这样的'.select('AccountID,AccountName,Contacts.Count()')',但我不认为OData / BreezeJS支持这一点。大声考虑也许解决方案是创建另一个名为AccountsWithContactCounts之类的WebService方法并返回一个自定义类,但理想情况下,如果可以在breezejs客户端中执行此操作,我希望避免这种情况。

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


推荐答案

正如Steve确认的答案是在Breeze控制器上创建新方法

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

这篇关于BreezeJs导航属性计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆