c# - LINQ to SQL exception: Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator -
i know duplicate on so, can't figure out how use contains operator in specific code:
i have 5 bookings in database:
id, booking, pren, reservationcode
1, visithere, 1, 1000a
2, visithere, 1, 1000a
3, visithere, 1, 1000a
4, visitthere, 2, 2000a
5, visitthere, 2, 2000a
public int specialdelete(datacontext db, ienumerable<bookingtype> bookings) { var rescodes = (from b in bookings select b).distinct().toarray(); // code breaks here ienumerable<bookingtype> bookingstodelete = db.gettable<bookingtype>().where(b => bookings.any(p => p.pren == b.pren && p.reservationcode == b.reservationcode)); int deleted = bookingstodelete.count(); db.gettable<bookingtype>().deleteallonsubmit(bookingstodelete); db.submitchanges(); return deleted; } when pass first record method (1, visithere, 1, 1000a), want retrieve ids 1,2 , 3, not 4 , 5.
i can matching pren , reservationcode.
how can .any , .all operators throwing above exception?
note: method must accept list of bookings because argument multiple bookings passed method, used single booking example.
edit: need linq2sql generate bunch of sql statements (let's want delete records in db):
delete bookings b b.reservationcode = '1000a' , b.pren = 1 delete bookings b b.reservationcode = '2000a' , b.pren = 2
the error getting trying direct use .contains method passing in simple array. default translates array in clause in format:
where foo in ("b1", "b2", "b3")
notice here can't multi-dimentional array in in clause (as need do). since can't join server side local array, options become limited long have composite key relationship.
if don't need fetch rows in order delete them, faster anyway use context's executecommand issue deletes. make sure parameterize query (see http://www.thinqlinq.com/post.aspx/title/does-linq-to-sql-eliminate-the-possibility-of-sql-injection)
string deletequery = "delete bookings b b.reservationcode = {0} , b.pren = {1}"; foreach (var bookingtype in bookings) { db.executecommand(deletequery, bookingtype.reservationcode, bookingtype.preen); }
Comments
Post a Comment