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 ?

2

2 Answers 2

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

db.GuestRSVPs.DeleteAllOnSubmit(guestrsvps);
db.SubmitChanges();
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.