c# - IQueryable for entities .Where( property is in local array) -


so know iqueryables translated sql statements , cannot handle possible methods might put clause.

but i'm trying do:

int[] alreadyselectedids = ... var subjects = entities.newinstance.subjects.where(x => array.indexof(alreadyselectedids, x.id) == -1).tolist(); 

and reading post these below, i'm comforted ef5 should able translate this.
getting entities keys match list(or array) of ids
using linq query int ids array

however, i'm getting error:

linq entities not recognize method 'int32 indexof[int32](int32[], int32)' method, , method cannot translated store expression.

and googling error not give me help.

i have tried

var newsubjects = entities.newinstance.subjects.where(x => alreadyselectedids.contains(x.id)).tolist(); 

unable create null constant value of type 'system.int32[]'. entity types, enumeration types or primitive types supported in context.

and

list<int> alreadyselectedids = ... 

unable create null constant value of type 'system.collections.generic.list`1'. entity types, enumeration types or primitive types supported in context.

i'm stuck , brain getting mushy beyond possibility type of graceful recovery. can kindly save me?

entities.newinstance.subjects.where(x => alreadyselectedids.contains(x.id)).tolist(); 

should work, if alreadyselectedis not null

you can null check inside or before query :

entities.newinstance.subjects.where(x => alreadyselectedids == null                                           ? true // or false                                           : alreadyselectedids.contains(x.id)                                     ).tolist(); 

(which can rewritten, depending if want or nothing if alreadyselectedids null)

//return if null x => alreadyselectedids == null || alreadyselectedids.contains(x.id) 

or

//return nothing if null x => alreadyselectedids != null  && alrreadyselectedids.contains(x.id) 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -