In Habanero how would I restrict the number of objects returned from a database
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
p开发者_如何学编程ublic 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 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);
}
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.
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);
精彩评论