Oliver Hanappi
Oliver Hanappi

Reputation: 12336

How to query a subproperty with NHibernate's criteria api?

I would like to make a query which needs to compare an property's property with some value. For example:

... WHERE Identity.Location.Room = "room #1"

How can I achieve this with criteria api?

Best Regards
Oliver Hanappi

Upvotes: 1

Views: 497

Answers (2)

Denis
Denis

Reputation: 11

Try not use numbers in the alias name.

    var criteria = session.CreateCriteria(typeof(Identity))
    .CreateAlias("Location", "Al")
    .Add(Restrictions.Eq("Al.Room", "room #1"));

Upvotes: 1

Nigel
Nigel

Reputation: 2160

This will perform a query by joining your tables:

var criteria = session.CreateCriteria(typeof(Identity))
    .CreateAlias("Location", "l")
    .Add(Restrictions.Eq("l.Room", "room #1"));

Upvotes: 3

Related Questions