再现“DELETE NOT IN” 通过 LINQ/Subsonic 的 SQL 语句

发布于 2024-07-27 01:47:31 字数 473 浏览 3 评论 0原文

我想做一些类似 DELETE FROM TABLE WHERE ID NOT IN (1,2,3) AND PAGEID = 9

我有一个 IDS 列表,但如果需要的话可以更改。 我不知道如何为 LINQ 解析器获取布尔结果。

我认为这就是 Subsonic 所期望的。

db.Delete(content => content.PageID == ID).Execute();

我不知道如何执行 NOT IN 语句。 我尝试过 List.Contains 方法,但有些不太正确。

更新:一种替代方法是:

var items = TABLE.Find(x => x.PageID == ID)'
foreach(var item in items)
{
   item.Delete();
}

这对数据库的影响更大

I want to do something like DELETE FROM TABLE WHERE ID NOT IN (1,2,3) AND PAGEID = 9

I have a List of IDS but that could be changed if needs be. I can't work out how to get a boolean result for the LINQ parser.

Here is what Subsonic expects I think.

db.Delete(content => content.PageID == ID).Execute();

I can't work out how to do the NOT IN statement. I've tried the List.Contains method but something not quite right.

UPDATE: One alternative is to do:

var items = TABLE.Find(x => x.PageID == ID)'
foreach(var item in items)
{
   item.Delete();
}

This hits the database a lot more though

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

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

发布评论

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

评论(3

酒几许 2024-08-03 01:47:31

当你说“有些事情不太对劲”时,你到底是什么意思?

我希望这样写:

List<int> excluded = new List<int> { 1, 2, 3 };
db.Delete(content => !excluded.Contains(content.PageID)).Execute();

请注意,您需要对排除值数组而不是候选值调用 Contains 。 换句话说,不是说“项目不在集合中”,而是说“集合不包含项目”。

When you say "something not quite right" what exactly do you mean?

I'd expect to write:

List<int> excluded = new List<int> { 1, 2, 3 };
db.Delete(content => !excluded.Contains(content.PageID)).Execute();

Note that you need to call Contains on the array of excluded values, not on your candidate. In other words, instead of saying "item not in collection" you're saying "collection doesn't contain item."

溺渁∝ 2024-08-03 01:47:31

尝试 .Contains:(

db.Delete(content => content.PageID.Contains(<Array containing ID's>).Execute();

以上只是一个示例,可能需要根据您的具体情况进行一些修改)

Try .Contains:

db.Delete(content => content.PageID.Contains(<Array containing ID's>).Execute();

(the above is just an example, might need some polishing for your specific situation)

〃安静 2024-08-03 01:47:31

我发现这确实有效,但不是通过 LINQ

var table = new WebPageContentTable(_db.DataProvider);
var g = new SubSonic.Query.Delete<WebPageContent(_db.DataProvider)
            .From(table)
            .Where(table.ID)
            .NotIn(usedID)
            .Execute();

我发现这确实有效,并且通过 LINQ - 但它多次访问数据库。

        var f = WebPageContent.Find(x => !usedID.Any(e => e == x.ID));
        if (f.Count > 0)
        {
            var repo = WebPageContent.GetRepo();
            repo.Delete(f);
        }

我想这可以在一次数据库命中中起作用,但我在 QueryVisitor::VisitUnary 中抛出异常

WebPageContent.Delete(x => !usedID.Any(e => e == x.ID));

I have found that this works but its not via LINQ

var table = new WebPageContentTable(_db.DataProvider);
var g = new SubSonic.Query.Delete<WebPageContent(_db.DataProvider)
            .From(table)
            .Where(table.ID)
            .NotIn(usedID)
            .Execute();

I have found that this does work and via LINQ - however it hits the database multiple times.

        var f = WebPageContent.Find(x => !usedID.Any(e => e == x.ID));
        if (f.Count > 0)
        {
            var repo = WebPageContent.GetRepo();
            repo.Delete(f);
        }

This I imagine would work in one hit to the database but I get an exception thrown in QueryVisitor::VisitUnary

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