在 Habanero 中,我如何限制从数据库返回的对象数量
我需要限制从数据库返回的客户 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我现在无法测试这一点,但您应该可以通过使用 LoadWithLimit() 方法来测试这一点。 TotalRecords 变量将保存找到的总结果数,以防您想要包含一些信息,例如“显示 20 个 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.".
Till 是在正确的轨道上,但没有给出正确的语法。
代理用于从当前数据访问器加载集合,而不是创建新集合。所以代码将是:
应该可以了!
请将答案授予蒂尔,而不是我。他做了最多的研究。我刚刚更正了语法:)
编辑:
在下面关于丑陋的 API 的评论之后,我将包括对该方法的建议更改,以使其看起来更干净(感谢您的建议@GloryDev):
第二个参数是排序依据的字段,这对于有限制的获取是必要的任何意义。希望这有帮助。
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:
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):
The Second parameter is the field to order by, which is necessary for a fetch with limits to make any sense. Hope this helps.
安德鲁
你总是可以这样做看起来更整洁一些,但确实涉及解析
客户名称。
Andrew
You could always do this looks a bit neater but does involve parsing the
CustomerName.