Solution with too many LINQ parameters

Source: Internet
Author: User

There was an application in the project where the ID of the employee was first organized by the permission filter, then use LINQ to query the data in the appropriate table, provided that the IDs of the data from these queries must be in the ID that was filtered by the permission just now, but if there are too many IDs in the permissions, the problem is the incoming tabular data stream ( TDS) Remote Procedure Call (RPC) protocol stream is incorrect. Too many parameters are provided in this RPC request. Should be up to 2100 ". I infer that it may be the reason that the parameters in SQL Server cannot be used more than 2100. Please see the example below for the above description.

int[] inputlist = new int[2100];
for (int i = 0; i < inputlist.length; i++)
{
Inputlist[i] = i;
}
Dataclasses1datacontext dc = new Dataclasses1datacontext ();
var t= (from X in Dc.test1
Select x). Where (C=>inputlist.contains (C.testid));
dc. Log = Console.Out;
foreach (var item in t)
{
Console.WriteLine (Item.testid);
}

Running the code above will report that the incoming tabular data stream (TDS) Remote Procedure Call (RPC) protocol stream is incorrect. Too many parameters are provided in this RPC request. Should be up to 2100 "exception, if you change the inputlist array size to 5, execute SQL as follows:

SELECT [T0]. [TestID], [t0]. [Testcontent], [t0]. [P]
from [dbo]. [Test1] As [t0]
WHERE [t0].[ TestID] In (@p0, @p1, @p2, @p3, @p4)

Now the question is, if there are more than 2,100 parameters, I'm not thinking of a good way to do it with LINQ, which uses a compromise approach.

 public static list<t> test<t> () 
{
Dataclasses1datacontext dc = new DataClasses 1DataContext ();
DC. Log = Console.Out;
int[] inputlist = new int[2100];
StringBuilder sqlstr = new StringBuilder ("SELECT * from");
Sqlstr. Append (typeof (T). Name);
Sqlstr. Append ("Where TestID in (");
for (int i = 0; i < inputlist.length; i++)
{
Sqlstr. Append (i);
if (i < inputlist.length-1)
{
Sqlstr. Append (",");
}
Else
{
Sqlstr. Append (")");
}
}
SqlCommand cmd = new SqlCommand (sqlstr. ToString (), (SqlConnection) DC. Connection);
DC. Connection.Open ();
SqlDataReader dr = cmd. ExecuteReader ();
List<t> t = DC. Translate<t> (DR). ToList ();
return t;
}
static void Main (string[] args)
{
List<test1> L = myclass.test<test1> () ;
foreach (var item in L)
{
Console.WriteLine (item.testid);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.