Search code examples
c#linqpad

How to delete DB records without loading them into memory in LINQPad?


LINQPad allows to delete records from DB like this

var ids = new [] { 1, 2, 3 ... N };
var records = Table.Where(t => ids.Contains(t.Id)).ToList();

Table.DeleteAllOnSubmit(records);
SubmitChanges();

However it is overkill to load N records if needed to delete by Ids.

Installing EF or some other package? No native way?

@JoeAlbahari?


Solution

  • Create entities with these IDs and attach them to the table. After make a deletion query.

    var ids = new[] { 1, 2, 3... N };
    
    var records = ids.Select(x => new Record { Id = x }).ToArray(); // entity in Table
    
    Table.AttachAll(records);
    
    Table.DeleteAllOnSubmit(records);
    SubmitChanges();