If you manipulate the string, every time you change the dates you will get a slightly different SQL statement. It may look the same, but the string is different and will have a different hash. So this subverts caching of the execution plan and kills performance.
You can't really pass parameters in an IN
clause the way you're thinking. There are some other ideas in this post that might work, but if you read the notes they all say they have lousy performance.
If you can be assured that you will have a fixed number of inputs, you could use the technique of passing them as separate parameters:
... WHERE EventDate IN (@dt1, @dt2, @dt3)
Otherwise, I would go with an approach like Tom suggested, pass them as a delimited string and parse it on the server.
Also - since you are doing an exact match, you probably want to snap to midnight using DateTime.Today
rather than DateTime.Now
, or use the .Date
property of an existing DateTime.
Here's another suggestion, if you really are just trying to delete for the last three days, why not do this?
string sql = "DELETE FROM EventCalendar WHERE EventDate BETWEEN {0} AND {1}";
context.ObjectContext.ExecuteStoreCommand(sql,
new object[] { DateTime.Today.AddDays(-3), DateTime.Today.AddDays(-1) });
Or with a bit of thought, you could probably just use sql getdate()
and similar functions instead of passing parameters at all.