开发者

Using Castle ActiveRecord to query from multiple tables

So I've been using Castle ActiveRecord for my late开发者_如何转开发st 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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜