Question

How to pass a dynamic list of int to FromSql method? i tried the following code but it not works.

Conversion failed when converting the nvarchar value '1,2,3' to data type int.

var result = dataContext.Customers.FromSql($"SELECT * FROM Customer WHERE Id IN ({string.Join(",", listOfInt)})")
1
0
2/15/2020 1:04:21 PM

Popular Answer

You can try this way

var Ids = string.Join(',', listOfInt.Select(n => n.ToString()).ToArray());

var result = dataContext.Customers.FromSql($"SELECT * FROM Customer WHERE Id IN ({Ids})")
0
2/15/2020 2:10:37 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow