System.Linq.Dynamic - 我可以在WHERE语句中使用IN子句 [英] System.Linq.Dynamic - Can I use IN clause in WHERE statement
问题描述
我的动态LINQ WHERE语句:
I have dynamic linq WHERE statement:
dataContext.Table.Where("id = 0 Or id = 1 Or id = 2 Or ...");
我想更改为:
I want change to:
dataContext.Table.Where("id IN (0, 1, 2, ...)");
但doesn't工作。我怎样才能做到这一点获得更好的性能?
But it doesn´t work. How can I do this for better performance?
推荐答案
//edit: this is probably broken, see below
ids = new int[] {1,2,3,4};
dataContext.Table.Where("id.Contains(@0)", ids);
旁白:这是使用动态LINQ EX pressions占位符好的做法。否则,你可能会打开自己LINQ注入式攻击(是注射可以通过动态LINQ?)
编辑:
其实我觉得我搞砸这件事。
不幸的是我不能在此刻进行测试。
但我认为在这种情况下,正确的语法应该是 dataContext.Table.Where(@ 0.Contains(ID),IDS);
,而不是其他的方式,该版本做的不可以工作外的开箱即用的。
actually I think I messed this up.
Unfortunately I cannot test this at the moment.
But I think the correct syntax in this case should be dataContext.Table.Where("@0.Contains(id)",ids);
, not the other way around, and that version does not work out-of-the-box.
请参阅<一href="http://blog.walteralmeida.com/2010/05/advanced-linq-dynamic-linq-library-add-support-for-contains-extension-.html">here一种方式,以这种功能添加到动态链接。您需要修改库这一点。
See here for a way to add this functionality to dynamic link. You need to modify the library for this.
这篇关于System.Linq.Dynamic - 我可以在WHERE语句中使用IN子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!