I am not an expert of SQL and what I am trying to achieve is the following.
I have a list of integers called PersonIds. First of all I need to transform this list of integers to a list of strings. Why? Because as far as I have understood for the WHERE .. IN condition in SQL this is the kind of variable I need to feed to the query.
Then I need to modify the list of strings in order to prevent SQL injection and therefore inserting @ before every Id of the list.
At last I create the query and feed the list I just created.
What I tried to do is:
var listIds = string.Join(",@", PersonIds, 0, PersonIds.Count()));
var query = $"DELETE FROM PersonTable WHERE PersonId IN (@{listIds})";
There is something I am doing wrong. To recap I need to steps:
- preparing a list (including mechanism to avoid SQL injection) that I need to feed to the query
- create the query using as argument the list I created
Thanks in advance!
PersonIds.Count? Is it ever likely to be > 2000?