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