IQueryable的的实体。凡(属性是在本地阵列) [英] IQueryable for entities .Where( property is in local array)

查看:214
本文介绍了IQueryable的的实体。凡(属性是在本地阵列)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我知道,iQueryables被转换成SQL语句,因此无法处理,你可能投入where子句中的所有可能的方法。

但是,这就是我想要做的:

  INT [] alreadySelectedIds = ...
变种受试者= Entities.NewInstance.Subjects.Where(X => Array.IndexOf(alreadySelectedIds,x.Id)==  -  1).ToList();
 

和阅读后这样的下方,我安慰了EF5应该能够把这种。
<一href="http://stackoverflow.com/questions/7011033/getting-entities-whose-keys-match-listor-array-of-ids">Getting实体的按键匹配列表(或数组)的IDS
使用LINQ to查询诠释IDS从数组

不过,我得到这个错误:

  

LINQ到实体不能识别方法的Int32   的IndexOf [的Int32](的Int32 [],的Int32)'方法,和这种方法不能   翻译成一家商店前pression。

和谷歌上搜索这个错误并没有给我太多的帮助。

我也尝试

  VAR newSubjects = Entities.NewInstance.Subjects.Where(X =&GT; alreadySelectedIds.Contains(x.Id))。了ToList();
 

  

无法创建类型'System.Int32的[]'空恒定值。只要   实体类型,枚举类型或原始类型的支持   这方面

 名单,其中,INT&GT; alreadySelectedIds = ...
 

  

无法创建类型的空常量'System.Collections.Generic.List`1。只有实体类型,枚举类型或基本类型在这方面的支持。

我坚持我的大脑越来越肉麻超出允许任何类型的优雅的恢复。谁能好心救我?

解决方案

  Entities.NewInstance.Subjects.Where(X =&GT; alreadySelectedIds.Contains(x.Id))。了ToList( );
 

应该工作,如果 alreadySelectedIs 不为空

您可以做一个空检查内部或查询之前:

  Entities.NewInstance.Subjects.Where(X =&GT; alreadySelectedIds == NULL
                                         ?对或错
                                         :alreadySelectedIds.Contains(x.Id)
                                    ).ToList();
 

(可重写,这取决于如果你想全有或全无,如果 alreadySelectedIds 为null)

  //返回所有,如果空
X =&GT; alreadySelectedIds == NULL || alreadySelectedIds.Contains(x.Id)
 

 如果空//返回任何结果
X =&GT; alreadySelectedIds = NULL和放大器;!&安培; alrreadySelectedIds.Contains(x.Id)
 

So I know that iQueryables are translated into SQL statements and thus cannot handle all possible methods that you might put into a where clause.

But this is what I'm trying to do:

int[] alreadySelectedIds = ...
var subjects = Entities.NewInstance.Subjects.Where(x => Array.IndexOf(alreadySelectedIds, x.Id) == -1).ToList();

And reading post like these below, I'm comforted that EF5 should be able to translate this.
Getting Entities whose keys match list(or array) of ids
Using LINQ To Query Int Ids From An Array

However, I'm getting this error:

LINQ to Entities does not recognize the method 'Int32 IndexOf[Int32](Int32[], Int32)' method, and this method cannot be translated into a store expression.

And googling this error does not give me much help.

I have also tried

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.

and

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.

I'm stuck and my brain is getting mushy beyond the possibility for any type of graceful recovery. Can anyone kindly save me?

解决方案

Entities.NewInstance.Subjects.Where(x => alreadySelectedIds.Contains(x.Id)).ToList();

should work, if alreadySelectedIs is not null

you can do a null check inside or before your query :

Entities.NewInstance.Subjects.Where(x => alreadySelectedIds == null 
                                         ? true // or false 
                                         : alreadySelectedIds.Contains(x.Id)
                                    ).ToList();

(which can be rewritten, depending if you want all or nothing if alreadySelectedIds is null)

//return all if null
x => alreadySelectedIds == null || alreadySelectedIds.Contains(x.Id)

or

//return nothing if null
x => alreadySelectedIds != null  && alrreadySelectedIds.Contains(x.Id)

这篇关于IQueryable的的实体。凡(属性是在本地阵列)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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