在 Habanero 中,我如何限制从数据库返回的对象数量

发布于 2024-11-13 11:39:55 字数 587 浏览 0 评论 0原文

我需要限制从数据库返回的客户 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);
    }

I need to restrict the number of customer bo's returned from the database as I am searching for a partial customer name and at the moment I get over 600 bo's when searching for 'a'. I would like to restrict this to 20. My code at the moment is

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乄_柒ぐ汐 2024-11-20 11:39:55

我现在无法测试这一点,但您应该可以通过使用 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);
}

I cannot test this right now but you should be able to this by using the LoadWithLimit() method. The totalRecords variable will hold the number of total results found in case you want to include some info like "Showing 20 of totalRecords results.".

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);
}
灯角 2024-11-20 11:39:55

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 的评论之后,我将包括对该方法的建议更改,以使其看起来更干净(感谢您的建议@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;
}

第二个参数是排序依据的字段,这对于有限制的获取是必要的任何意义。希望这有帮助。

Till is on the right track there, but did not give the correct syntax.
The broker is used to load collections from the current Data Accessor, not for creating new ones. So the code would then be:

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

That should do it!
Please award the answer to Till, not me. He did the most research. I just corrected the syntax :)

EDIT:
After comments below about the ugly API, I will include suggested changes to the method to make it look cleaner (Thanks for your suggestion @GloryDev):

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

The Second parameter is the field to order by, which is necessary for a fetch with limits to make any sense. Hope this helps.

够钟 2024-11-20 11:39:55

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

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

Andrew
You could always do this looks a bit neater but does involve parsing the
CustomerName.

        var totalRecords = 0;
        Broker.GetBusinessObjectCollection<Customer>("CustomerName Like partialCustomerName ", "CustomerName", 0, 20, out totalRecords);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文