Search code examples
c#.netnhibernatehqlcastle-activerecord

C# + Castle ActiveRecord: HasAndBelongsToMany and collections


Let's say I have many-to-many relationship (using the ActiveRecord attribute HasAndBelongsToMany) between Posts and Tags (domain object names changed to protect the innocent), and I wanted a method like

FindAllPostByTags(IList<Tag> tags)
that returns all Posts that have all (not just some of) the Tags in the parameter. Any way I could accomplish this either with NHibernate Expressions or HQL? I've searched through the HQL documentation and couldn't find anything that suited my needs. I hope I'm just missing something obvious!


Solution

  • You could also just use an IN statement

    DetachedCriteria query = DetachedCriteria.For<Post>();
    query.CreateCriteria("Post").Add(Expression.In("TagName",  string.Join(",",tags.ToArray()) );
    

    I haven't compiled that so it could have errors