【问题标题】:In Habanero how would I restrict the number of objects returned from a database在 Habanero 中,我将如何限制从数据库返回的对象数量
【发布时间】:2011-06-02 13:07:48
【问题描述】:

我需要限制从数据库返回的客户 bo 的数量,因为我正在搜索部分客户名称,而目前在搜索“a”时我得到了 600 多个 bo。我想将其限制为 20。我目前的代码是

    public IEnumerable<Customer> FindCustomers(string partialCustomerName)
    {
        if (string.IsNullOrEmpty(partialCustomerName)) 
          throw new ArgumentException("partialCustomerName must be at least one character long");
        var criteria = Criteria.Create<Customer, string>(cust =>   cust.CustomerName, Criteria.ComparisonOp.Like, partialCustomerName + "%");
        return Broker.GetBusinessObjectCollection<Customer>(criteria);
    }

【问题讨论】:

    标签: c# orm habanero


    【解决方案1】:

    我现在无法对此进行测试,但您应该可以使用 LoadWithLimit() 方法进行测试。 totalRecords 变量将保存找到的总结果数,以防您想包含一些信息,例如“显示 20 个 totalRecords 结果”。

    public IEnumerable<Customer> FindCustomers(string partialCustomerName)
    {
        if (string.IsNullOrEmpty(partialCustomerName)) 
          throw new ArgumentException("partialCustomerName must be at least one character long");
    
        var criteria = Criteria.Create<Customer, string>(cust =>   cust.CustomerName, Criteria.ComparisonOp.Like, partialCustomerName + "%");
        int totalRecords;
        return Broker.GetBusinessObjectCollection<Customer>().LoadWithLimit(criteria, 0, 20, ref totalRecords);
    }
    

    【讨论】:

    • 感谢这项工作,但它似乎可以调用数据库。
    • 是的,这将调用数据库并在代理方法上加载所有客户,然后使用条件和限制重新加载集合。
    【解决方案2】:

    Till 在正确的轨道上,但没有给出正确的语法。 代理用于从当前数据访问器加载集合,而不是用于创建新的。所以代码将是:

    public IEnumerable<Customer> FindCustomers(string partialCustomerName)
    {
        if (string.IsNullOrEmpty(partialCustomerName)) 
          throw new ArgumentException("partialCustomerName must be at least one character long");
    
        var criteria = Criteria.Create<Customer, string>(cust =>   cust.CustomerName, Criteria.ComparisonOp.Like, partialCustomerName + "%");
        int totalRecords;
        var col = new BusinessObjectCollection<Customer>();
        col.LoadWithLimit(criteria, "CustomerName", 0, 20, ref totalRecords);
        return col;
    }
    

    应该这样做! 请将答案授予蒂尔,而不是我。他做的研究最多。我刚刚更正了语法:)

    编辑: 在下面关于丑陋 API 的 cmets 之后,我将包括对方法的建议更改以使其看起来更干净(感谢您的建议 @GloryDe​​v):

    public IEnumerable<Customer> FindCustomers(string partialCustomerName)
    {
        if (string.IsNullOrEmpty(partialCustomerName)) 
          throw new ArgumentException("partialCustomerName must be at least one character long");
        var col = new BusinessObjectCollection<Customer>();
        col.LoadWithLimit("CustomerName Like " + partialCustomerName + "%", "CustomerName", 20);
        return col;
    }
    

    第二个参数是排序依据的字段,它对于具有限制的提取有意义。希望这会有所帮助。

    【讨论】:

    • 优秀的正是我想要的。
    • 是的,我更喜欢下面答案中的那个。这个使用 lambdas 的 Create.Criteria 东西是非常新的,据我所知,这是 Linq to Habanero 在幕后使用的东西。我不确定这是否打算成为 API。
    • 我同意@Chris 的观点。 API 的这种用法非常抽象,看起来一点也不干净。 lambda 的东西实际上是为了解析完整的 lambda 表达式,而不仅仅是获取属性名称。我在帖子末尾添加了上述方法的更简洁版本,其中包括@GloryDe​​v 的建议并稍作修改。
    • 和 @GloryDe​​v 老实说 Criteria 部分还可以,它不是很好(用奇怪的方式定义操作),这是可怕的核心 API new BusinessObjectCollection&lt;Customer&gt;();, col.LoadWithLimit 这些东西是纯粹的WTF,看到下面的工厂方法甚至更大 WTF Broker.GetBusinessObjectCollection&lt;Customer&gt;().LoadWithLimit( 几乎任何时候 API 要求您使用他们创建的集合类,这表明架构师失败了。但是对于 ORM,它是灰色区域之一,因为他们可以权衡需要做动态代理
    【解决方案3】:

    安德鲁 你总是可以这样做看起来更整洁,但确实涉及解析 客户姓名。

            var totalRecords = 0;
            Broker.GetBusinessObjectCollection<Customer>("CustomerName Like partialCustomerName ", "CustomerName", 0, 20, out totalRecords);
    

    【讨论】:

    • 啊,太棒了,我看到 Broker 也支持这种语法。与上面的解决方案相比,我更喜欢这种语法:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-13
    • 1970-01-01
    • 2019-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多