如何从实体框架集合中删除项目子集

发布于 2024-12-10 12:21:17 字数 563 浏览 0 评论 0原文

我有一个实体框架EntityCollection

我需要从数据库中删除与给定 where 子句匹配的所有项目。这是我现有的代码:

// Perform the deletes
foreach (var deleteReq in order.Requirements.Where(x=>!orderContract.Requirements.Any(y=>y.RequirementId==x.RequirementId)))
{
    order.Requirements.Remove(deleteReq);
}

基本上,我试图从 order.Requirements 集合中删除不在 orderContract.Requirements 集合中的任何内容(与 Id 匹配)。

正如您可能猜到的,此代码会抛出异常,因为我正在修改正在迭代的集合。

通常我只会使用 RemoveAll()EntityCollection 不支持该方法。

那么...我怎样才能删除我需要的所有记录呢?

I have an entity framework EntityCollection.

I need to delete all items that match a given where clause from the database. This is my existing code:

// Perform the deletes
foreach (var deleteReq in order.Requirements.Where(x=>!orderContract.Requirements.Any(y=>y.RequirementId==x.RequirementId)))
{
    order.Requirements.Remove(deleteReq);
}

Basically I am trying to remove anything from the order.Requirements collection that is not in the orderContract.Requirements collection (matching on an Id).

As you may guess, this code throws and exception because I am modifying the collection I am iterating.

Normally I would just use RemoveAll() but the EntityCollection does not support that method.

So... How can I delete all the records I need to?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑着哭最痛 2024-12-17 12:21:17

我创建了一个单独的列表,它似乎有效:

// Perform the deletes
var reqsToDelete = order.Requirements.Where(x=>!orderContract.Requirements.Any(y=>y.RequirementId==x.RequirementId)).ToList();
foreach (var deleteReq in reqsToDelete)
{
    order.Requirements.Remove(deleteReq);
}

这样,当我从 order.Requirements 列表中删除该项目时,它不会影响我正在迭代的列表。

I created a seperate list and it seems to have worked:

// Perform the deletes
var reqsToDelete = order.Requirements.Where(x=>!orderContract.Requirements.Any(y=>y.RequirementId==x.RequirementId)).ToList();
foreach (var deleteReq in reqsToDelete)
{
    order.Requirements.Remove(deleteReq);
}

That way, when I remove the item from the order.Requirements list, it is not affecting the list that I am iterating.

你没皮卡萌 2024-12-17 12:21:17

将要删除的所有需求实体收集到列表中。

然后从需求实体集合中而不是从 order.Requirements 中删除其中的每一项。

collect all the Requirement entities you want to delete into a list.

Then remove each one of those from the Requirements entity collection rather than from order.Requirements.

黯然#的苍凉 2024-12-17 12:21:17

在 EF6 之前,有一个 RemoveRange 方法可以提供更好的性能和更干净的 API

var deleteQuery = order
    .Requirements
    .Where(x=> !orderContract.Requirements.Any(y=>y.RequirementId == x.RequirementId));

order.Requirements.RemoveRange(deleteQuery);

现在,EF 不会在删除之前加载每个项目,因为这是已接受答案中的情况。

Prior to EF6, there is a RemoveRange method for better performance and cleaner API

var deleteQuery = order
    .Requirements
    .Where(x=> !orderContract.Requirements.Any(y=>y.RequirementId == x.RequirementId));

order.Requirements.RemoveRange(deleteQuery);

Now, the EF wont load each item before remove, as it is a case in the accepted answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文