linq to sql批处理删除 [英] linq to sql batch delete

查看:59
本文介绍了linq to sql批处理删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据库: 具有IdPosts,也具有IdTags和具有TagsToPosts.PostId => Posts.IdTagsToPosts.TagId => Tags.Id FK关系的TagsToPosts表. 我需要通过以下方式从TagsToPosts中删除多个项目. 我通过解析字符串来创建IList<Tag> newTags.每个标签都有其名称.我想删除所有指向单个帖子(TagsToPosts.PostId == mypostid)且指向Tag且名称不在我newTags中的TagsToPosts项目.

I have the following DB: Posts which have an Id, Tags also with Id, and TagsToPosts table which have TagsToPosts.PostId => Posts.Id and TagsToPosts.TagId => Tags.Id FK relations. I need to delete multiple items from TagsToPosts in following way. I'm creating IList<Tag> newTags by parsing a string. Each tag have it's name. I want to delete all TagsToPosts items pointing to single post (TagsToPosts.PostId == mypostid) and which points to Tag with name which not in my newTags.

例如,我有一个帖子,带有Id = 1,三个标签:1 => "tag1", 2 => "tag2", 3 => "tag3"和ManyToMany关系表TagsToPosts:1 => 1, 1 => 2, 1 => 3 因此,所有三个标签都链接到我的帖子. 之后,我将通过解析字符串创建一个新的IList<Tag> newList = new List<Tag>(). newList包含:0 => "tag1", 0 => "tag2". 现在,我想从表TagToPosts中删除第三个关系,因为我的新标记列表不包含名称为"tag3"的标记.所以我需要找到一个区别.我知道我可以使用JOIN找到类似的物品,但是如何找到区别呢?

For instance I have one post with Id = 1, three tags: 1 => "tag1", 2 => "tag2", 3 => "tag3" And ManyToMany relations table TagsToPosts: 1 => 1, 1 => 2, 1 => 3 So all three tags are linked to my post. After that I'll create a new IList<Tag> newList = new List<Tag>() by parsing a string. newList contains: 0 => "tag1", 0 => "tag2". Now I want to remove third relation from table TagsToPosts because my new list of tags doesn't contain tag with name "tag3". So I need to find a difference. I know I can find similar items using JOIN but how to find difference?

我希望这可以在一个数据库查询中发生,而无需遍历每个项目以将其删除.

I want this to happend in one DB query without iterating over each item to delete it.

推荐答案

您无法使用LINQ-to-SQL做到这一点.

You can't do this with LINQ-to-SQL.

LINQ-to-SQL不适用于批处理操作-它不能进行批处理插入,不能进行批处理更新,也不能进行批处理删除.集合中的每个对象都将单独处理.您可以在一个事务中完成所有操作,但是每条记录始终会有一个查询.

LINQ-to-SQL is not good for batch operations - it can't do batch inserts, it can't do batch updates, and it can't do batch deletes. Every object in your collection is treated individually. You can do all the operations in one transaction, but there will always be a query for each record.

MSDN a>

一个更好的选择是编写一个存储过程,该存储过程将执行您想要的操作.

A better option is to write a stored procedure that will do what you want.

这篇关于linq to sql批处理删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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