Hello,

 

The bit you're missing is a CreateAlias (or CreateCriteria) call - the
snippet below should do the trick.

 

Session.CreateCriteria<Watcher>()

       .Add(Restrictions.Eq("UserId", userId))

       .CreateAlias("Message", "msg")

       .Add(Restrictions.Gt("msg.Created", DateTime.Now))

       .List<Watcher>();

 

Three points to note:

 

1.  Restrictions.Xxx is preferred to Expression.Xxx

2.  If you have the User object then you can pass that into the Criteria
instead of referencing an ID in code

3.  You may want to consider using the QueryOver API which gets rid of
the need for 'magic strings' in the code.  A query with QueryOver which
I believe will give the same results would look like this (it's not
actually the same query, because the identical construct would use a
JoinAlias but that can get a bit messy code-wise IMO).  

 

Session.QueryOver<Watcher>()

       .Where(x => x.User == user)

       .JoinQueryOver<Message>(x => x.Message)

       .Where(x => x.Created >= DateTime.Now)                 // This
where clause refers to the JoinQueryOver, i.e. the Message object

       .List();

 

Pete

 

 

From: [email protected] [mailto:[email protected]] On
Behalf Of EmptyNull
Sent: 09 September 2012 16:12
To: [email protected]
Subject: [nhusers] How to sort with createcriteria trough the colum of a
reference

 

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.

Reply via email to