LINQ to SQL,大IN语句,2100参数限制 [英] LINQ To SQL, Large IN Statement, 2100 Parameter Limitation

查看:285
本文介绍了LINQ to SQL,大IN语句,2100参数限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,



我正在尝试获取行的子集在基于我拥有的ID列表的表中,使用LINQ to SQL。




类似于:



List< int> fooIdList; //这可能有1000个Ids。


List< Foo> filteredFooList = myDataContext.Foo.Where(foo => fooIdList.Contains(foo.FooId))。ToList();




问题是,id列表中可能包含数千个id - 这会导致抛出异常,据我所知,这是因为SQL Server没有不允许超过2100个参数。




有没有办法让它在LINQ中工作?



此外,由于2100参数限制,使用LINQ to SQL时还有什么需要注意的吗?




谢谢,


< p align = left> Jonathan

解决方案

如果我正确理解问题,你最好不要插入你的ID需要匹配到一个单独的表中并使用内部联接来查找两个表之间的匹配。



这是一个示例:



将在查询中使用的两个表







































USERS


选择


ID


<强>名称


ID


0


Tom


0


1


Harry


3


2


Bob


5


3


Greg


2


4


Sam


5


John


...


...



然后执行此查询





代码块

Hey,

 

I'm trying to get a subset of the rows in a table based on a list of ids that I have,  using LINQ to SQL.

 

 

Something like:

 

List<int> fooIdList; //This might have 1000s of Ids in it.

List<Foo> filteredFooList = myDataContext.Foo.Where(foo => fooIdList.Contains(foo.FooId)).ToList();

 

 

The problem is, the id list might have many thousands of ids in it -- this causes an exception to be thrown which, to my understanding, is because SQL Server doesn't allow more than 2100 parameters.

 

 

Is there any way to get this to work in LINQ? 

 

Also, is there anything else to watch out for while using LINQ to SQL due to the 2100 parameter limitation?

 

 

Thanks,

Jonathan

解决方案

If I am understanding the problem correctly, you would be better off inserting the IDs you need to match against into a separate table and using an Inner Join to find matches between the two tables.

 

Here is an example:

 

Two tables that will be used in the query

USERS

SELECTION

ID

Name

ID

0

Tom

0

1

Harry

3

2

Bob

5

3

Greg

2

4

Sam

5

John

...

...

 

Then this query is executed

Code Block


这篇关于LINQ to SQL,大IN语句,2100参数限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆