0

This code I received from another stackoverflow post from Agent Shark and changed it slightly for my use. Problem is I get an error:

Conversion failed when converting the nvarchar value '39432,39431,39430,39429,39428' to data type int.

Question is, how do I fix this problem?

The original StackOverFlow Post:

What is the best method to access a large database using MVC 4 and Entity Framework 6

Here is the code I'm using:

public void DeleteBatchCampaignContacts(IList<int> ids)
{
    if (ids == null) return;
    if (ids.Count == 0) return;

    var idsToDelete = new StringBuilder();

    foreach (var id in ids)
    {
        idsToDelete.AppendFormat(",{0}", id);
    }

    db.Database.ExecuteSqlCommand("DELETE FROM EmailContactCampaign WHERE EmailContact_EmailContactId in (@contactIds)",
    new SqlParameter("@contactIds", idsToDelete.ToString().Remove(0, 1)));
}
Community
  • 1
  • 1
user2789697
  • 227
  • 1
  • 7
  • 13

1 Answers1

1
Database.ExecuteSqlCommand(string.Format("DELETE FROM CampainContacts WHERE CampaignId in ({0})", idsToDelete.ToString().Remove(0, 1)));
Agent Shark
  • 517
  • 2
  • 6