Hi,
You would do something like this:
var results = Session.CreateCriteria<Watcher>()
.CreateAlias("Message", "message", JoinType.InnerJoin)
.Add(Restrictions.Eq("User.Id", userId))
.Add(Restrictions.Gt("message.Created", DateTime.Now))
.List();
or using the QueryOver-api:
var results = Session.QueryOver<Watcher>()
.Where(w => w.User.Id == userId)
.JoinQueryOver(w => w.Message)
.Where(m => m.Created > DateTime.Now)
.List();
Note however that using the criteria api or the query over api you will also in
this case load the Message entities. So you might want to do this as a subquery
if you only want the "Watcher" entities like so:
var subQuery = DetachedCriteria.For<Watcher>("watcher")
.CreateAlias("Message", "message",
JoinType.InnerJoin)
.Add(Restrictions.Eq("User.Id", userId))
.Add(Restrictions.Gt("message.Created",
DateTime.Now))
.SetProjection(Projections.Property("watcher.Id"));
var results = Session.CreateCriteria<Watcher>()
.Add(Subqueries.PropertyIn("Id", subQuery))
.List();
You don't need to do a subquery with HQL queries though:
var results = Session.CreateQuery("from Watcher w where w.User.Id = :userId and
w.Message.Created > :date")
.SetParameter("userId", userId)
.SetParameter("date", DateTime.Now)
.List<Watcher>();
Cheers,
Ted
9 sep 2012 kl. 17:12 skrev EmptyNull <[email protected]>:
> Hello nhusers,
>
> I would like to know the syntax used to evaluate an expression from a
> referenced table.
>
> Ie:
>
> User:
> UserId
>
> Watcher
> UserId
> MessageId
>
> Message
> MessageId
> Created
>
> return Session.CreateCriteria<Watcher>()
> .Add(Expression.Eq("UserId", userId))
> .Add(Expression.Gt("Message.Created", DateTime.now) <---
> Don't know how to reference the message and get the created column.
> .List<Watcher>();
>
> Googling hasn't given me any example syntax so I thought I would ask in here.
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/nhusers/-/dvxEMXGHB_sJ.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.