nHibernate query to outer join
Given this nhibernate query:
var cats = _nhibernateSession.QueryOver<SavedSearchResult>().Where(x => x.UserId == userId && x.IsDeleted == false).JoinQueryOver<SearchCategory>(x => x.Searc开发者_高级运维hCategory)
.List<SavedSearchResult>().ToList().GroupBy(c => c.SearchCategory.Id);
This works well but if a category does not have any search results, then we dont get the categories back.
Can anyone suggest an approach to this?
An outer join is expressed via QueryOver
like this:
IQueryOver<Cat,Kitten> catQuery =
session.QueryOver<Cat>()
.Left.JoinQueryOver(c => c.Kittens)
.Where(k => k.Name == "Tiddles");
(from the QueryOver docs on NHForge.org)
So, in your case, this will be:
var cats =
_nhibernateSession.QueryOver<Categories>()
.Left.JoinQueryOver(x => x.SearchCategory)
.Where (c => c.UserId == userId && c.IsDeleted = false );
I've omitted the group by clause, since I do not see any aggregated columns in your select. (Haven't tested the query as well though).
精彩评论