tags:

views:

132

answers:

2

I have this simple query:

ICriteria crit = session.CreateCriteria(typeof(Article));

crit.CreateCriteria("Category", global::NHibernate.SqlCommand.JoinType.InnerJoin)
        .Add(Restrictions.Eq("Name", "Fun"));

This is returning all the articles in the "Fun" category. My question is, how can I add a simple OR clause to return also any article with "Title" = "New Joke" even if it is not on the "fun" category?

Seems i cant get this working...

Edit: I know I have to swith to LeftOuterJoin, the question is about the correct syntax to build this query.

Edit2: To make this more clear, the sql query that i'm trying to build would be:

SELECT article.* 
FROM article LEFT OUTER JOIN category ON (article.category = category.id)
WHERE category.name = 'fun' OR article.title = 'new joke'
+1  A: 

You need to use an Or criteria like so:

crit.Add( Expression.Or(
    Expression.Eq("Category.Name", "Fun"),
    Expression.Eq("Title", "New Joke")
    ));
David M
Does not work: "could not resolve property: Category.Name"
Drevak
Worked using createAlias instead, thanks!
Drevak
+1  A: 

You can just use the normal C# OR operator:

Restrictions.Eq("Category.Name", "Fun") || Restrictions.Eq("Title", "New Joke")
Mauricio Scheffer