4

How i can write a query , so that foreach will not use. My current query is :

IEnumerable<GuestRSVP> guestrsvps = db.GuestRSVPs.Where(p => p.CeremonyGuestPartyId == CeremonyGuestpartyId);
            foreach (var grsvp in guestrsvps)
            {
                db.GuestRSVPs.DeleteObject(grsvp);
            }

How i can delete all objects in a single query, without using foreach loop ?

  • Have a look at this: http://stackoverflow.com/questions/654561/linq-to-sql-batch-delete – Allov Aug 15 '12 at 13:35
  • I think this SO question will help you http://stackoverflow.com/questions/853526/using-linq-to-remove-objects-within-a-listt – juan.facorro Aug 15 '12 at 13:36

2 Answers2

6
var guestrsvps = db.GuestRSVPs
                   .Where(p => p.CeremonyGuestPartyId == CeremonyGuestpartyId);

db.GuestRSVPs.DeleteAllOnSubmit(guestrsvps);
db.SubmitChanges();
Tim S. Van Haren
  • 8,861
  • 2
  • 30
  • 34
0

Try to use Delete method and pass lambda predicate into. Snippet should look like:

db.GuestRSVPs.Delete(p => p.CeremonyGuestPartyId == CeremonyGuestpartyId);

LINQ to SQL Extension: Batch Deletion with Lambda Expression

Ilya Ivanov
  • 23,148
  • 4
  • 64
  • 90