且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

System.Linq.Dynamic - 我可以在WHERE语句中使用IN子句

更新时间:2023-11-14 19:34:40

如何使用包含或喜欢的动态LINQ查询?

//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.

请参阅here一种方式,以这种功能添加到动态链接。您需要修改库这一点。

See here for a way to add this functionality to dynamic link. You need to modify the library for this.