使用LINQ删除表中的行到SQL [英] Deleting rows in a table with Linq to SQL

查看:121
本文介绍了使用LINQ删除表中的行到SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有哪些有场ID,groupId和MEMBERID表groupmembers体育数据库。我从一个来自所谓的CheckBoxList和txtRemoveGroupMember的GROUPID文本框中MEMBERID。现在我想删除至极既有GROUPID和MEMBERID行。
我已经试过这段代码:

I have a sports database with a table groupmembers which have the fields ID, groupID and memberID. I get the memberID from a textbox called txtRemoveGroupMember and the groupID from a checkboxlist. Now i want to delete the rows wich has both the groupID and the memberID. I have tried this code:

foreach(ListItem listItem in cblRemoveMemberFromGroup.Items)
{
       int memberid = Convert.ToInt32(txtRemoveGroupMember.Text);
       int groupid = Convert.ToInt32(listItem.Value);

       var removeFromGroup = from gm in dataContext.GroupMembers
            where (gm.memberID == memberid) && (gm.groupID == groupid)
            select gm;

       dataContext.GroupMembers.DeleteOnSubmit(removeFromGroup);
       dataContext.SubmitChanges();
}



但是我得到这个错误:错误7参数1:不能从转换'系统.Linq.IQueryable'到'GSI_side.GroupMember

But i get this error: Error 7 Argument 1: cannot convert from 'System.Linq.IQueryable' to 'GSI_side.GroupMember'

和这个错误:错误6的最好重载方法匹配'System.Data.Linq.Table.DeleteOnSubmit(GSI_side .GroupMember)'有一些无效参数

And this error: Error 6 The best overloaded method match for 'System.Data.Linq.Table.DeleteOnSubmit(GSI_side.GroupMember)' has some invalid arguments

希望有人能帮助我想出解决办法!

Hope someone can help me figure this out!

推荐答案

您必须调用.ToList()

You have to call .ToList()

var items = removeFromGroup.ToList();
foreach (var item in items)
  dataContext.GroupMembers.DeleteOnSubmit(item);

有关批量删除我使用的这个,因为LINQ to SQL的首次加载它是要删除的整个数据,那么它的每个删除每个

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

这篇关于使用LINQ删除表中的行到SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆