Friday, November 22, 2013

Add a condition on joined entities in NHibernate

This query is working, but the cross-cutting concern, e.g., localization, is cluttering the main Where clause

var x = 
    (from q in 
        from ps in session.Query<GetProductSold>()
        join pl in session.Query<ProductLanguage>() on ps.ProductId equals pl.ProductLanguageCompositeKey.ProductId
        select new { ps, pl }
    where q.pl.ProductLanguageCompositeKey.LanguageCode == languageCode
    select q).Cacheable();


We can convert that to following, however NHibernate doesn't support this yet:
var x = 
    (from q in 
         from ps in session.Query<GetProductSold>()
         join pl in session.Query<ProductLanguage>().Where(plc => plc.ProductLanguageCompositeKey.LanguageCode == languageCode) on ps.ProductId equals pl.ProductLanguageCompositeKey.ProductId
         select new { ps, pl }                         
     select q).Cacheable();


We have to do it this way:
var x =
    (from q in
         from ps in session.Query<GetProductSold>()
         join pl in session.Query<ProductLanguage>() on new { ps.ProductId, LanguageCode = languageCode } equals new { pl.ProductLanguageCompositeKey.ProductId, pl.ProductLanguageCompositeKey.LanguageCode }
         select new { ps, pl }                         
     select q).Cacheable();

No comments:

Post a Comment