Deleting rows in a table with Linq to SQL
removeFromGroup
is still of type IQuerable
.
You need to specify an actual GroupMember
to delete.
You could use
GroupMember removeFromGroup = (from gm in dataContext.GroupMembers
where (gm.memberID == memberid) && (gm.groupID == groupid)
select gm).SingleOrDefault();
dataContext.GroupMembers.DeleteOnSubmit(removeFromGroup);
dataContext.SubmitChanges();
Alternatively, if your query returns a collection (from the looks of it, it won't since you are filtering by memberId
) you could use
List<GroupMember> removeFromGroup = (from gm in dataContext.GroupMembers
where (gm.memberID == memberid) && (gm.groupID == groupid)
select gm).ToList();
dataContext.GroupMembers.DeleteAllOnSubmit(removeFromGroup);
dataContext.SubmitChanges();
You have to call .ToList()
var items = removeFromGroup.ToList();
foreach (var item in items)
dataContext.GroupMembers.DeleteOnSubmit(item);
For batch deletes I use this, because LINQ to SQL first loads the entire data which is going to be deleted, then it does the deletes each by each.
https://terryaney.wordpress.com/2008/04/14/batch-updates-and-deletes-with-linq-to-sql/
https://github.com/longday/LINQ-to-SQL-Batch-Updates-Deletes
var listToRemove=(from a in DB.Table
where a.Equals(id)
select a).ToList();
DB.Table.DeleteAllOnSubmit(listToRemove);
DB.SubmitChanges();