Search code examples
c#nhibernatequeryover

NHibernate QueryOver where value is IN list property/field?


I have a very basic model:

public class Blog : UniqueEntity<Guid>
{
    public virtual string Name { get; set; }
    public virtual string Tagline { get; set; }
    public virtual string ActiveThemeName { get; set; }
    public virtual string MainDomain { get; set; }
    public virtual IList<string> DomainAliases { get; set; } = new List<string>();
}

I need to write an NHibernate QueryOver query WHERE "hostname string" IN DomainAliases.

I've found a lot of answers here on SO on how I would do this in the reverse direction, ie: `WHERE DomainAliases CONTAINS "hostname string" but none for what I need.


Solution

  • The solution, as described here:

    NHibernate: Select item with entry in element bag

    should be like this:

    var demos = this.session.CreateCriteria<Blog>()
        .CreateAlias("DomainAliases", "d")
    
        // .elemnts is what we need
        .Add(Restrictions.Eq("d.elements", "hostname string"))
    
        .List<Blog>();
    

    Also check the:

    NHibernate How do I query against an IList property?