C# 使用 Linq to SQL 删除表中的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9503008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:39:25  来源:igfitidea点击:

Deleting rows in a table with Linq to SQL

c#asp.netsqllinq

提问by Twistar

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:

我有一个带有表 groupmembers 的体育数据库,其中包含字段 ID、groupID 和 memberID。我从名为 txtRemoveGroupMember 的文本框中获取 memberID,从复选框列表中获取 groupID。现在我想删除具有 groupID 和 memberID 的行。我试过这个代码:

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();
}

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

但我收到此错误:错误 7 参数 1:无法从“System.Linq.IQueryable”转换为“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

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

Hope someone can help me figure this out!

希望有人能帮我解决这个问题!

采纳答案by Adrian Iftode

You have to call .ToList()

你必须调用 .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.

对于批量删除,我使用this,因为 LINQ to SQL 首先加载将要删除的整个数据,然后逐个删除。

https://terryaney.wordpress.com/2008/04/14/batch-updates-and-deletes-with-linq-to-sql/

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

https://github.com/longday/LINQ-to-SQL-Batch-Updates-Deletes

回答by Jeremy Wiggins

removeFromGroupis still of type IQuerable.

removeFromGroup仍然是 类型IQuerable

You need to specify an actual GroupMemberto delete.

您需要指定GroupMember要删除的实际值。

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

或者,如果您的查询返回一个集合(从它的外观来看,它不会,因为您正在过滤memberId),您可以使用

List<GroupMember> removeFromGroup = (from gm in dataContext.GroupMembers
                                     where (gm.memberID == memberid) && (gm.groupID == groupid)
                                     select gm).ToList();


dataContext.GroupMembers.DeleteAllOnSubmit(removeFromGroup);
dataContext.SubmitChanges();

回答by gilix

var listToRemove=(from a in DB.Table
             where a.Equals(id)
             select a).ToList();

DB.Table.DeleteAllOnSubmit(listToRemove);
DB.SubmitChanges();