0

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...

Jason Sturges
  • 15,855
  • 14
  • 59
  • 80
  • I believe that because you don't escape the ',' char it's trying to convert the whole string - maybe try with "1\, 2". – t3hn00b May 28 '12 at 07:39
  • What language is this? Also, it doesn't work because you're passing a *single* *string* parameter, and expecting SQL to somehow decide to split that into *multiple* *int* parameters - do you know of *any* language that has that behaviour? – Damien_The_Unbeliever May 29 '12 at 06:20

1 Answers1

0

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();
skink
  • 5,133
  • 6
  • 37
  • 58