I often see a list of disconnected Linq2Sql objects or keys that I need to reinstall from the Linq2Sql data context to update or delete in the database. If it was SQL, I would use INSQL in the sentence WHERE, but I'm stuck with what to do in Linq2Sql. Here is an example of what I would like to write:
public void MarkValidated(IList<int> idsToValidate)
{
using(_Db.NewSession())
{
var items = _Db.Items.ThatAreIn(idsToValidate).ToList();
foreach(var item in items)
item.Validated = DateTime.Now;
_Db.SubmitChanges();
}
}
Or:
public void DeleteItems(IList<int> idsToDelete)
{
using(_Db.NewSession())
{
var items = _Db.Items.ThatAreIn(idsToValidate);
_Db.Items.DeleteAllOnSubmit(items);
_Db.SubmitChanges();
}
}
Can I do this in one trip to the database? If so, how? Is it possible to send all these ints to the database as a list of parameters and is it more efficient than doing foreach on the list to select each element at a time?
source
share