frenchie
frenchie

Reputation: 51927

deleting list of records with linq-to-sql

I'm writing a linq-to-sql query to delete a list of records: I pass in a list of RecordID and I want the corresponding records deleted.

This is what I have so far:

public void DeleteMyRecords(List<long> TheRecordIDs)
{
   using (TheDC MyDC = new TheDC()) //TheDC is the DataContext 
   {
       MyDC.DeleteOnSubmit(from a in MyDC.TheTable
                           where TheRecordIDs.Contains(a.RecordID)
                           select a).SubmitChanges(); 
   }
}

For now, I see the DeleteOnSubmit underlined in red. How do you write such query so that it works?

Thanks for your help.

Upvotes: 2

Views: 7570

Answers (1)

Adil
Adil

Reputation: 148110

As query may return more then one record you may need to call DeleteAllOnSubmit

var records = from a in MyDC.TheTable
                           where TheRecordIDs.Contains(a.RecordID)
                           select a;

MyDC.TheTables.DeleteAllOnSubmit(records); 
MyDc.SubmitChanges();

Upvotes: 3

Related Questions