I'm trying to use LinqToSQL to generate a IN clause that could contain a lot of items. My original code was something like:
context.Employees.Where(p => EmployeeIDs.Contains(p.EmployeeID));
Which ends up generating an in clause with each employee id as a SQL parameter. The problem is if you go over 2100 parameters, an exception is thrown. I've worked around this elsewhere in my application by executing multiple queries and combining the results. That works ok in simple cases, but I'm doing something a little more complex now and I don't think this is the best solution.
Anyway, the Contains generates something like
EmployeesID IN (@p0, @p1, @p2)
But is there a way to convince it to do this?
EmployeesID IN (23, 582, 3948)
I started going down the path of using the dynamic query example, but then realized that it seems to be only for writing dynamic linq code, not dynamic sql. Then I found ExecuteQuery, but I don't want to build my entire query string manually, just the particular sections of the query that I want.
To be more specific, I'd like something like:
context.Employees
.Where(p => p.Active == true)
.Where("EmployeeID IN (23, 582, 3948)");
Or preferrably (if this would generate IN without using parameters):
context.Employees
.Where(p => p.Active == true)
.Where(p => p.In("EmployeeID", EmployeeIDs));
Any way to do this? Thanks.