title: How can I use Nhibernate to retrieve data when the "WHERE IN()" have thousands of values? (too many parameters in the sql)
body: <p><strong>The problem</strong>: Nhibernate parses each value in the "WHERE IN()" sql as parameters and MS SQL server doesn't support enough parameters (over 2000).</p>
  
  <p>I am using Nhibernate with Linq to retrive my data from the SQL server and I need to load alot of entities based on already known ID's.</p>
  
  <p>My code looks something like this:</p>
  
  <pre><code>int[] knownIds = GetIDsFromFile();
  var loadedEntities = _Repository.GetAll()
                                  .Where(x =&gt; knownIds.Contains(x.ID))
                                  .ToList();
  </code></pre>
  
  <p>Which give an sql like this:</p>
  
  <pre><code>SELECT id, name FROM MyTable 
  WHERE id IN (1 /* @p0 */,2 /* @p1 */,3 /* @p2 */,4 /* @p3 */, 5 /* @p4 */)
  </code></pre>
  
  <p>If there is too many values in <em>knownIds</em>, then this code will throw an Exception because of the many parameters that NHibernate uses.</p>
  
  <p>I think the best solution would be if I could make NHibernate use only 1 parameter for the whole "WHERE IN()", but I don't know how to do this:</p>
  
  <pre><code>SELECT id, name FROM MyTable WHERE id IN (1, 2, 3, 4, 5 /* @p0 */)
  </code></pre>
  
  <p>I'll be glad to hear any ideas of how to solve this - either by extending the LINQ provider or by other means. One solution is to simple do the query x times (knownIds.Count / 1000), but I rather want a generic solution which would work for all my entities.</p>
  
  <p>I have tried looking at extending the LINQ provider by searching google and Stackoverflow, however I can't find a solution and I dont have any experience with either HQL or the treebuilder.
  Here are a few of the sites I have been at:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/6267912/extending-linq-to-nhibernate-provider-in-combination-with-dynamic-linq-problem">Extending LINQ to Nhibernate provider, in combination with Dynamic LINQ problem</a></li>
  <li><a href="http://fabiomaulo.blogspot.com/2010/07/nhibernate-linq-provider-extension.html" rel="noreferrer">NHibernate LINQ provider extension</a></li>
  <li><a href="http://www.symbolsource.org/Public/Metadata/Default/Project/NHibernate/3.0.0.GA/Release/All/NHibernate/NHibernate/Linq/Functions/QueryableGenerator.cs" rel="noreferrer">Sourcecode: NHibernate/Linq/Functions/QueryableGenerator.cs</a></li>
  </ul>
  
  <p><strong>UPDATE:</strong> 
  I know it ain't good practice by having so many values in the IN clause, but I don't know a better solution for what I want to do.<br>
  Consider a company where all the customers pay for the company's services one time each month. The company don't handle the payments itself, but have another company to collect the money. One time each month the company receives a file containing the status of these payments: if they have been paid or not. The file only contains the ID of the specific payment, and not the ID of the customer.
  A company with 3000 monthly customers, will then make 3000 LogPayments each month, where the status need to be updated. After 1 year there will be around 36.000 LogPayments, so just loading them all doesn't seem like a good solution either.</p>
  
  <p><strong>MY SOLUTION:</strong>
  Thanks for all the usefull answers. In the end I choosed to use a combination of the answers. For this specific case I did something like Fourth suggested, as that would increase performance a great deal. However I have allso implemented the generic method Stefan Steinegger suggested, because I like that I can do this, if that is what I really want. Besides, I don't want my program to crash with an exception, so in the future I will allso use this ContainsAlot-method as a safeguard.</p>