- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
所以我知道 iQueryables 被翻译成 SQL 语句,因此无法处理您可能放入 where 子句中的所有可能方法。
但这就是我想要做的:
int[] alreadySelectedIds = ...
var subjects = Entities.NewInstance.Subjects.Where(x => Array.IndexOf(alreadySelectedIds, x.Id) == -1).ToList();
阅读下面这些帖子,我很欣慰 EF5 应该能够翻译它。
Getting Entities whose keys match list(or array) of ids
Using LINQ To Query Int Ids From An Array
但是,我收到了这个错误:
LINQ to Entities does not recognize the method 'Int32 IndexOf[Int32](Int32[], Int32)' method, and this method cannot be translated into a store expression.
用谷歌搜索这个错误并没有给我太多帮助。
我也试过
var newSubjects = Entities.NewInstance.Subjects.Where(x => alreadySelectedIds.Contains(x.Id)).ToList();
Unable to create a null constant value of type 'System.Int32[]'. Only entity types, enumeration types or primitive types are supported in this context.
和
List<int> alreadySelectedIds = ...
Unable to create a null constant value of type 'System.Collections.Generic.List`1'. Only entity types, enumeration types or primitive types are supported in this context.
我被困住了,我的大脑变得糊涂,无法进行任何类型的优雅恢复。有好心人救救我吗?
最佳答案
Entities.NewInstance.Subjects.Where(x => alreadySelectedIds.Contains(x.Id)).ToList();
应该工作,如果 alreadySelectedIs
不为空
您可以在查询内部或之前进行空检查:
Entities.NewInstance.Subjects.Where(x => alreadySelectedIds == null
? true // or false
: alreadySelectedIds.Contains(x.Id)
).ToList();
(如果 alreadySelectedIds
为 null,则可以重写,具体取决于您是想要全部还是什么都不想要)
//return all if null
x => alreadySelectedIds == null || alreadySelectedIds.Contains(x.Id)
或
//return nothing if null
x => alreadySelectedIds != null && alrreadySelectedIds.Contains(x.Id)
关于c# - IQueryable for entities .Where(属性在本地数组中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18618082/
我是一名优秀的程序员,十分优秀!