Unfortunately, SQL Server can not a handle a parameter list which exceeds 2100 parameters. I have a couple of queries which when run in a batch mode exceed this limit. To still get the results I want and quickly cycle through the data set I have decided to make use of Skip(i).Take(2000) in a for loop.
What I was not prepared for was that both Union and Concat require an IEnumerable to be more than just instantiated to work. Sure it’s an empty, but I must be missing something fundamental about their use. At any rate, to solve the issue I ended up using a List and AddRange. I am making use of NHibernate, but don’t think that factors into why Union and Concat aren’t working.
var machineResults = new List<Machine>();
for (int i = 0; i < machines.Count(); i += 2000)
{
// I would have expected Union or Concat to work here
machineResults.AddRange(GetSession().CreateQuery(
@"select distinct m
from Machine m
where m in (:MachinesList)") // there's more criteria than this
.SetParameterList("MachinesList",
machines.Skip(i).Take(2000).ToList())
.List<Machine>());
}
return machineResults;
So what am I missing about Union and Concat? Why don’t they work in the above statement? I understand why AddRange works, but what if I was concerned about the uniqueness of my results and wanted to use Union?
Union and Concat are pure methods. They do not modify the object they are run on, instead they return a copy.
After this code has run
a => [1, 2, 3, 4]
b => [5, 6, 7, 8]
c => [1, 2, 3, 4,5, 6, 7, 8]
Meaning if you had
machineResults.concat(x), you’re not actually modifying the machineResults collection. Instead you’d have to havemachineResults = machineResults.concat(x).