使用 Castle ActiveRecord 从多个表中查询

发布于 2024-09-14 11:20:11 字数 629 浏览 2 评论 0原文

所以我一直在我的最新项目中使用 Castle ActiveRecord,并且在很大程度上我非常喜欢它。现在我试图弄清楚如何使用 AR 执行简单的联接查询,但我没有看到它。

所以我有一个文章表,一篇文章可以有许多与之关联的标签。这是 Article 类中的属性:

[HasAndBelongsToMany(typeof(Tag), Table="ArticleTags", ColumnKey="ArticleID", ColumnRef="TagID")]
    public IList<Tag> Tags {
            get
            {
                return m_tagList;
            }
            set
            {
                m_tagList = value;
            }
    }

有一个 Tag 类,其中包含标签的名称和 ID。 AR 无需显式类即可处理所需的桥接/关联表。

所以现在我只想获取具有给定标签名称的所有文章。到目前为止,我还没有弄清楚如何在 AR 中做到这一点。 SQL 很简单。尝试为 AR 进行重现,但没有那么多。

任何帮助将不胜感激。

So I've been using Castle ActiveRecord for my latest project and for the most part I like it a lot. Now I'm trying to figure out how to execute a simple join query with AR and I'm not seeing it.

So I have an Article table and an article could have many tags associated to it. This is the attribute in the Article class:

[HasAndBelongsToMany(typeof(Tag), Table="ArticleTags", ColumnKey="ArticleID", ColumnRef="TagID")]
    public IList<Tag> Tags {
            get
            {
                return m_tagList;
            }
            set
            {
                m_tagList = value;
            }
    }

There's a Tag class that contains the name and ID of the tag. There's the required bridge/association table which AR can handle without an explicit class.

So now I just want to get all articles that have a given tag name. Thus far, I haven't figured how to do that in AR. The SQL is easy. Trying to reproduce for AR, not so much.

Any help here would be much appreciated.

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

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

发布评论

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

评论(1

年少掌心 2024-09-21 11:20:11

好吧,发布此文后不久,我找到了答案。

我使用了 DetachCriteria 对象,然后将其放入我的 Article.SlicedFindAll() 函数中。

这是代码。

DetachedCriteria query = DetachedCriteria.For<Article>()
            .CreateCriteria("Tags")
                .Add(Expression.Eq("Name", tag));

整个下午都在这上面。 叹息

Well, shortly after posting this, I found my answer.

I used a DetachCriteria object and then put that into my Article.SlicedFindAll() function.

Here's the code.

DetachedCriteria query = DetachedCriteria.For<Article>()
            .CreateCriteria("Tags")
                .Add(Expression.Eq("Name", tag));

Spent all afternoon on this. sigh

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