The original question by mail is getting old, but I have encountered this problem several times recently and found a solution that could help others. And, since none of the answers above gave an answer with NHibernate code, I also think that this is relevant to the post.
In our software product, users make choices in the user interface. Then the front end passes the list of matching identifiers to the back end for queries and data manipulation.
First, I needed a function to split the list of elements into sections of 1000 elements.
Note that this is VB.NET, but the function itself was found elsewhere on StackOverflow in C #:
Public Shared Iterator Function Partition(Of T)(source As IList(Of T), Optional size As Int32 = 1000) As IEnumerable(Of List(Of T)) For i As Integer = 0 To CInt(Math.Ceiling(source.Count / CDbl(size))) Yield New List(Of T)(source.Skip(size * i).Take(size)) Next End Function
I used this function in several ways. One way is to loop over sections of the list to modify QueryOver to create a union of all results, for example:
Dim allPartitions As IEnumerable(Of List(Of Integer)) = Partition(idList, SplitSize) Dim foundObjects As IEnumerable(Of MyEntity) = New List(Of MyEntity) For Each part As List(Of Integer) In allPartitions foundObjects = foundObjects.Union( _session.QueryOver(Of MyEntity) _ WhereRestrictionOn(Function(x) x.ID).IsIn(part).Future()) Next
Another way I used is to create a Constraint that can be applied in QueryOvers. The following function creates such a restriction (ICriterion):
Public Shared Function GetRestrictionOnIds(ids As List(Of Integer), propertyName As String) As ICriterion Dim allParts As IEnumerable(Of List(Of Integer)) = Partition(ids, SplitSize) Dim restriction As Disjunction = Restrictions.Disjunction() For Each part As List(Of Integer) In allParts restriction.Add(Restrictions.In(propertyName, part)) Next Return Restrictions.Conjunction().Add(restriction) End Function
I call this function as follows:
Dim wellIdRestriction As ICriterion = GetRestrictionOnIds(wellIdsList, "WellId") Dim bleedOffs As IList(Of BleedOff) = _session.QueryOver(Of BleedOff)() _ .Where(wellIdRestriction) _ .Where(..... more restrictions...) _ .And(...yet more restrictions....) _ .List().GroupBy(...some function...) _ .ToDictionary(.. key-function, value-function...)