How to simulate "B" using Linq2Sql

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()) // Instatiates new DataContext
    {
        // ThatAreIn <- this is where I am stuck
        var items = _Db.Items.ThatAreIn(idsToValidate).ToList();
        foreach(var item in items)
            item.Validated = DateTime.Now;
        _Db.SubmitChanges();
    } // Disposes of DataContext
}

Or:

public void DeleteItems(IList<int> idsToDelete)
{
    using(_Db.NewSession()) // Instatiates new DataContext
    {
        // ThatAreIn <- this is where I am stuck
        var items = _Db.Items.ThatAreIn(idsToValidate);
        _Db.Items.DeleteAllOnSubmit(items);
        _Db.SubmitChanges();
    } // Disposes of DataContext
}

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?

+3
source share
1 answer

You can:

var items = _Db.Items.Where(i => idsToValidate.Contains(i.Key));

, - ?

+3

Source: https://habr.com/ru/post/1741164/


All Articles