c# - Error: The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities -


working on mvc5 app. have string array defined as....

string[] myarray; 

it has 3 items in it....

{string[3]}    [0]: "a411d1bc-21f7-4e4d-a4d3-4dd36e1b319f"    [1]: "ef4e3655-fa6f-4dfc-b2d4-178ac5914f0b"    [2]: "d75a98c5-a829-43c5-b2cf-d50be1189a05" 

these primary keys records in aspnetuser table. want execute ef clause using array items in multiple "where". (so want retrieve these 3 users aspnetuser table.) here's code far...

iqueryable<event> events = db.events;  if (myarray != null) {     events = events.include(a => a.aspnetuser);      for(int = 0; <= myarray.length-1; i++)     {         events = events.where(u => u.aspnetuser.id == myarray[i].tostring());     } } 

so, can see, i'm looping thru array (that contains pk) , using in clause.

but, right when hit first .where line in loop i'm getting error:

the linq expression node type 'arrayindex' not supported in linq entities.

what doing wrong here? there better approach?

finally, dynamic scenario such described, how can used or statements rather , commands where?

under normal circumstances, when performing operation where in entityframework, isn't executed in memory when operate on enumerable (like list). instead, converted sql executed db provider. means doing things, such using extension methods on objects or, in case, getting elements arrays index not option, converter cannot turn such things sql.

to fix existing code, need modify loop so:

for(int = 0; <= myarray.length-1; i++) {     var temp = myarray[i].tostring();     events = events.where(u => u.aspnetuser.id == temp); } 

moving array operation outside of query allows entityframework convert sql properly.

a better approach doing want following:

var result = events.where(u => myarray.contains(u.aspnetuser.id)); 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -