ExecuteStoreQuery and "WHERE IN ({0})

1k Views Asked by At

I got the following code:

string query = "select * from dbo.Personnel where PersonnelId in ({0})";
string param = "1, 2";
var test = model.ExecuteStoreQuery<Personnel>(query, param).ToList();

I get the following error: "Conversion failed when converting the nvarchar value '1, 2' to data type int."...

Does anyone know how to solve this? What I want is select * from dbo.Personnel where PersonnelId in (1, 2) but the numbers can be any numbers...

1

There are 1 best solutions below

0
On

Don't send param to ExecuteStoreQuery, just replace {0} with param

string param = "1, 2";
string query = string.Format("select * from dbo.Personnel where PersonnelId in ({0})", param);

or

string query = "select * from dbo.Personnel where PersonnelId in ({0})";
query = query.Replace("{0}",param);

var test = model.ExecuteStoreQuery<Personnel>(query).ToList();