NHibernate Left Outer Join

criterialeft-joinnhibernate

I'm looking to create a Left outer join Nhibernate query with multiple on statements akin to this:

SELECT 
    * 
FROM [Database].[dbo].[Posts] p
LEFT JOIN 
    [Database].[dbo].[PostInteractions] i
ON 
   p.PostId = i.PostID_TargetPost And i.UserID_ActingUser = 202       

I've been fooling around with the critera and aliases, but I haven't had any luck figuring out how do to this. Any suggestions?

Best Answer

I actually figured it out. You need to do a check for null

.CreateCriteria("Interactions", "i", NHibernate.SqlCommand.JoinType.LeftOuterJoin)
            .Add(Expression.Or(Expression.Eq("i.ActingUser", user), Expression.IsNull("i.ActingUser")))

this creates a left join on targetpost id and then eliminates all non null/non user interactions.

Related Topic