Linq Nhibernate离开了加盟

时间:2013-03-23 17:43:34

标签: linq nhibernate fluent-nhibernate linq-to-nhibernate

Theft具有动作属性

这是我试图让NHibernate.Linq产生的查询:

SELECT * FROM `thefts`
LEFT JOIN memberThefts
ON thefts.id = memberThefts.theftId AND memberThefts.memberId = 1

我想获取所有盗窃的列表,其中action.memberId ==某个数字或者如果它没有找到一行则为null,简单作为查询但是它整天都在给我一个噩梦!

        thefts = session.Query<Theft>()
            .Fetch(x => x.action)
            .Where(x => x.action.memberId == member.id)
            .ToList();

执行以下SQL:

select theft0_.id                 as id9_0_,
       memberthef1_.memberId      as memberId7_1_,
       theft0_.name               as name9_0_,
       theft0_.chance             as chance9_0_,
       memberthef1_.theftId       as theftId7_1_,
       memberthef1_.availableTime as availabl3_7_1_
from   thefts theft0_
       left outer join memberThefts memberthef1_
         on theft0_.id = memberthef1_.theftId,
       memberThefts memberthef2_
where  theft0_.id = memberthef2_.theftId
       and memberthef2_.memberId =1 /* ?p0 */

盗窃类:

    public class Theft
    {
        public virtual byte id { get; set; }
        public virtual string name { get; set; }
        public virtual byte rank { get; set; }
        public virtual byte chance { get; set; }
        public virtual MemberTheft action { get; set; }
...

它的映射:

public TheftMap()
{
    Table("thefts");
    Id(x => x.id);
    Map(x => x.name);
    Map(x => x.id);
    Map(x => x.chance);
    References(x => x.action)
        .Nullable()
        .PropertyRef(x => x.theftId)
        .Column("id");
}

任何解决方案都会执行HQL,QueryOver等

1 个答案:

答案 0 :(得分:5)

使用LINQ提供程序无法完成,但您可以使用QueryOver执行此操作。有点像:

MemberTheft memberAlias = null;
var result = Session.QueryOver<Theft>()
                    .Left.JoinQueryOver(x => x.action, () => memberAlias)
                    .Where(() => memberAlias.memberId == member.id);

编辑:更新查询。