2012-08-15 49 views
4

我如何寫一個查詢,以便foreach不會使用。我當前的查詢是:如何寫一個更好的LINQ到SQL查詢C#

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

如何刪除所有對象在一個單一的查詢,而無需使用foreach循環?

+0

看一看這樣的:http://stackoverflow.com/questions/654561/linq-to-sql-batch-delete – Allov 2012-08-15 13:35:08

+0

我覺得這太問題會幫助你http://stackoverflow.com/questions/853526/using-linq-to-remove-objects-within-a-listt – 2012-08-15 13:36:49

回答

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

db.GuestRSVPs.DeleteAllOnSubmit(guestrsvps); 
db.SubmitChanges();