nHibernate 3-Left Join re-Linq解决方案 [英] nHibernate 3 - Left Join re-Linq solution

查看:76
本文介绍了nHibernate 3-Left Join re-Linq解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用nHibernate 3在下面运行此Linq查询.

I am trying to to run this Linq query below with nHibernate 3.

var items = from c in session.Query<tbla>()
       join t in session.Query<tblb>() on c.Id equals t.SomeId into t1 // use left join on trades.
       from t2 in t1.DefaultIfEmpty()
select new {item = c, desc = t2.Description};

据我所知,这是在linq中执行左联接的常用方法.但是,这给了我一个不受支持的异常消息.如何在不求助于HQL的情况下实现基本的左联接?像nHibernate这样流行的ORM不能像左联接那样支持行人,这似乎有些愚蠢.

This is the stock way to perform a left join in linq to my knowledge. However it's giving me an unsupported exception message. How can I achieve a basic left join without resorting back to HQL? This seems somewhat silly that an ORM as prevalent as nHibernate cannot support something as pedestrian as a left join.

我已经在下面回答了我自己的问题.

I've put the real answer to my own question below.

推荐答案

经过进一步研究;使用 QueryOver 以强类型的方式可以实现(尽管并不明显)一个>.技巧是将外部查询别名变量与WithAlias和TransformUsing结合使用.这是一个没有过滤和排序的示例.

After further research on this; this is possible (although not obvious) to achieve in a strongly typed fashion using QueryOver. The trick is to use outer Query alias variables in conjunction with WithAlias, and TransformUsing. Here is an example that does left join with filtering and sorting.

// Query alias variables 
entityTypeA anchorType = null;
entityTypeB joinedType = null;

var items = session.Query<entityTypeA>( ()=>anchorType )
            .Left.JoinAlias(() => anchorType.FieldName, () => joinedType)
            .WithSubquery.WhereProperty(e => e.FieldD).In(myFilterList)
            // bind property mappings using WithAlias
            .SelectList(list => list
                        .Select(e => e.FieldNameA).WithAlias( ()=> anchorType.FieldNameA )
                        .Select(e => e.FieldNameB).WithAlias( ()=> anchorType.FieldNameB )
                        )
           .OrderBy(e => joinedType.FieldNameC).Desc
           .TransformUsing(Transformers.AliasToBean<entityTypeA>()) // transform result to desired type.
           .List<entityTypeA>();

这篇关于nHibernate 3-Left Join re-Linq解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆