How to write best linq for sql query c #

How can I write a request so that foreach will not use. My current request:

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

How can I delete all objects in a single request without using a foreach loop?

+4
source share
2 answers
 var guestrsvps = db.GuestRSVPs .Where(p => p.CeremonyGuestPartyId == CeremonyGuestpartyId); db.GuestRSVPs.DeleteAllOnSubmit(guestrsvps); db.SubmitChanges(); 
+6
source

Try using the Delete method and pass the lambda predicate. The snippet should look like this:

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

LINQ to SQL Extension: Batch Deletion with Lambda Expression

0
source

All Articles