views:

4279

answers:

3

from f in CUSTOMERS
where depts.Contains(f.DEPT_ID)
select f.NAME

depts is a list (IEnumerable<int>) of department ids

this query works fine until you pass a large list (say around 3000 dept ids) .. then i get this error: The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Too many parameters were provided in this RPC request. The maximum is 2100.

i changed my query to:
var dept_ids = string.Join(" ", depts.ToStringArray());
from f in CUSTOMERS
where dept_ids.IndexOf(Convert.ToString(f.DEPT_id)) != -1
select f.NAME

using IndexOf() fixed the error but made the query slow. is there any other way to solve this? thanks so much.

+1  A: 

Why not write the query in sql and attach your entity?

It's been awhile since I worked in Linq, but here goes:

IQuery q = Session.CreateQuery(@"
         select * 
         from customerTable f
         where f.DEPT_id in (" + string.Join(",", depts.ToStringArray()) + ")");
q.AttachEntity(CUSTOMER);

Of course, you will need to protect against injection, but that shouldn't be too hard.

Joel Potter
thanks joel. let me try it and i'll let you know how it goes.
ban-G
Caveat: that is OK with integers, but with strings: watch out for SQL injection.
Marc Gravell
Presumably you want a comma in there somewhere, Joel?
Marc Gravell
Yes, injection is always a concern when writing dynamic sql, but with integers you're safer. Comma added. ;)
Joel Potter
hi joel, thanks again for taking time to answer my question. :-)
ban-G
+3  A: 

How about like so (which batches it into manageable pieces). The other (non-LINQ) options involve CSV and a "split" UDF, and table-valued-parameters (in SQL2008).

Marc Gravell
this is also a good idea. i'll also try this out. thanks marc.
ban-G
sorry it took me forever to figure things out. i merely copied the codes from your previous post and it worked beautifully! THANK YOU SOOOOO MUCH!!!! :-)
ban-G
+1  A: 

You will want to check out the LINQKit project since within there somewhere is a technique for batching up such statements to solve this issue. I believe the idea is to use the PredicateBuilder to break the local collection into smaller chuncks but I haven't reviewed the solution in detail because I've instead been looking for a more natural way to handle this.

Unfortunately it appears from Microsoft's response to my suggestion to fix this behavior that there are no plans set to have this addressed for .NET Framework 4.0 or even subsequent service packs.

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=475984

UPDATE:

I've opened up some discussion regarding whether this was going to be fixed for LINQ to SQL or the ADO.NET Entity Framework on the MSDN forums. Please see these posts for more information regarding these topics and to see the temporary workaround that I've come up with using XML and a SQL UDF.

jpierson