Hi, I'm having some trouble querying using properties of associated objects.
To boil down to an easy example, let's say I've got a BlogPost class, and a User class. Each BlogPost belongs to a User.
BlogPost is mapped to User using a many-to-one relationship. However, I can't seem to retrieve ILists of BlogPosts belonging to a given User's username property.
I'd like to grab the list like this:
Code:
IList blogPosts = ListByPropertyValueOrderBy(Type.GetType(BlogPost.GetType().ToString(), "User.Username", (object)"sammy", "CreatedOn", "DESC");
Is it in fact considered legal to do that "User.UserName" in a situation like this?
Here's the retrieval method:
Code:
public IList ListByPropertyValueOrderBy(Type type, string propertyName, object propertyValue, string orderPropertyName, string orderValue)
{
try
{
ICriteria crit=m_session.CreateCriteria(type);
crit.Add(Expression.Eq(propertyName, propertyValue));
if(orderValue == "ASC")
{
crit.AddOrder(Order.Asc(orderPropertyName));
}
else if(orderValue == "DESC")
{
crit.AddOrder(Order.Desc(orderPropertyName));
}
return crit.List();
}
catch (Exception ex)
{
throw ex;
}
}
I've also tried it using HQL. This works:
Code:
IList blogPosts = m_session.CreateQuery( "from BlogPost as blogPost where blogPost.User.UserName = 'sammy'")
Any idea whether it's possible using the Criteria interface?