Dariusz Tarczynski
Dariusz Tarczynski

Reputation: 16711

NHibernate sub-collection using QueryOver

I have simple classes:

public class Order
{
    public int Id {get;set;}
    public IList<Name> Names{get;set;}
}

public class Name
{
    public int Id {get;set;}
    public int LangId {get;set;}
    public string LocalName {get;set;}
}

The quesetion is how to query subcollection of Order to get all that have some Order.Names.LocalName (something like this):

IList<Order> orders = repository.GetByProductLocalName("laptop");

I would like to use new NH3 feature QueryOver, not using Linq (I'v found some solutions in SO but all of them uses Linq).

Upvotes: 2

Views: 2161

Answers (1)

dotjoe
dotjoe

Reputation: 26940

All you need to do is declare an alias variable for the Names collection and then you can use that in the where clause...

String localName = "laptop";
Name namesAlias = null;

return session.QueryOver<Order>()
    .JoinAlias(x => x.Names, () => namesAlias)
    .Where(() => namesAlias.LocalName == localName)
    .TransformUsing(Transformers.DistinctRootEntity)
    .List<Order>();

Upvotes: 9

Related Questions