I have a postgresql database and what I am trying to do is remove a bunch of rows from a table in as few queries as possible. So looping is not a good option. I am using NPGSQL for the postgres driver.
I have the code below but it is not working.
string[] namesToDelete = PromosReplies.
PromosRepliesLoaded.GroupBy(pr => pr.Name).
Select(r=>r.Key).ToArray();
long[] repliesIdsToDelete = context.PromosReplies.
Where(pr => namesToDelete.Contains(pr.Name)).
Select(r => r.Idx).ToArray();
if (repliesIdsToDelete.Length > 0)
{
foreach (var name in namesToDelete)
{
context.Database.ExecuteSqlCommand("DELETE FROM messages WHERE name = {0}", name);
}
string idToDelete = String.Join(",", repliesIdsToDelete);
int result = context.Database.ExecuteSqlCommand(
"DELETE FROM message_translations WHERE idx IN ({0})",
repliesIdsToDelete);
I get a "ERROR: 22P02: invalid input syntax for integer:" error when trying to execute the last query. Is there a way to overcome this? If yes can something similar be done with first delete statement where I have to use a string?