c# - How to use parameters in Entity Framework in a "in" clause? -


i using entity framework 4.0 , want use following query:

to do following:

strsqlquery = "select * mytable iddata in (@ids)"; lstparameters.clear(); myparameter = new sqlparameter("@ids", strids); lstparameters.add(myparameter);  mycontext.mytable.sqlquery(strsqlquery, lstparameters.toarray<object>()).tolist<mytable>(); 

but exception not possible convert nvarchar bigint.

that because parameter type string, , ids in table bigint.

i try create list of long , add ids, other error.

how can use list o ids parameter in query?

i use parameters, if possible.

thanks.

there few problems code.

first off, fix data type error, you'd have convert strids integers before doing else. should work

var ids = strids.select(s => long.parse(s)); 

now, since you're using entity framework already, why not use linq instead of creating sql query?

var results =     r in mycontext.mytable     ids.contains(r.iddata)     select r; 

or in fluent syntax

var results = mycontext.mytable.where(r => strids.contains(r.iddata)); 

but if want use sql, in operator not support parameters that. you'd have write this:

strsqlquery = "select * mytable iddata in(@id1, @id2, @id3, ...)"; 

so without effort, can generate query ids this:

strsqlquery = "select * mytable iddata in(" + string.join(", ", ids.select((s, i) => "@id" + i)) + ")"; foreach(var parameter in ids.select((s, i) => new sqlparameter("@id" + i, s))) {     lstparametros.add(parameter); } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -