我有一个网站,以简单的表格形式显示来自SQL Server的数据,并带有过滤器、排序、分页等功能。我使用Fluent NHibernate作为ORM,查询代码如下:
public IList<Operation> GetResults(UserCommand command)
{
var result = Session.Query<Operation>();
if (command.Ids != null)
result = result.WhereRestrictionOn(o => o.Id).IsIn(command.Ids);
// ... other filters ...
return result.Skip(command.Page * command.PageSize).Take(command.PageSize).List();
}
问题在于
command.Ids
(以及其他一些输入参数)可能包含大量值。如果数量超过2100,则查询执行失败并出现以下错误。System.Data.SqlClient.SqlException: The incoming request has too many parameters. The server supports a maximum of 2100 parameters. Reduce the number of parameters and resend the request.
将查询分成小块,每个小块不超过2100个参数并不是一个选项,因为使用多个查询,我将无法知道要显示所需页面需要跳过和获取的记录数。
是否有其他方法来减少参数数量?
command.Ids
是用户输入的,完全不可预测。 - holdenmcgrohen