Linq Nhibernate left join

不羁的心 提交于 2019-12-20 07:52:28

问题


A Theft has an action property

This is the query i'm trying to get NHibernate.Linq to produce:

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

I want to get a list of all the thefts where action.memberId == some number or just null if it doesn't find a row, simple as a query yet it's been giving me a nightmare all day!

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

This executes the following 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 */

The theft class:

    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; }
...

And it's mapping:

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");
}

Any solution will do HQL, QueryOver etc


回答1:


It can't be done using the LINQ provider, but you can do it with QueryOver. Something along the lines of:

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

Edit: Updated Query.



来源:https://stackoverflow.com/questions/15590021/linq-nhibernate-left-join

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!