Linq Nhibernate离开了加入 [英] Linq Nhibernate left join
本文介绍了Linq Nhibernate离开了加入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
盗窃行为行为属性
这是我试图让NHibernate.Linq产生的查询:
SELECT * FROM`thefts`
LEFT JOIN memberThefts
ONfts.id = memberThefts.theftId AND memberThefts.memberId = 1
我想获得所有盗号行为,其中action.memberId ==某些数字或只是如果它没有找到一行,只是作为一个查询简单,但它一直给我一整天的噩梦!
盗窃= session.Query< Theft>()
.Fetch(x => x.action)
.Where(x => x.action.memberId == member.id)
.ToList();
执行以下SQL:
选择theft0_.id作为id9_0_,
memberthef1_.memberId作为memberId7_1_,
theft0_.name作为name9_0_,
theft0_.chance作为chance9_0_,
memberthef1_.theftId as theftId7_1_,
memberthef1_.availableTime as availabl3_7_1_
from theft 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盗窃
{
公共虚拟字节id {get;组; }
公共虚拟字符串名{get;组; }
public virtual byte rank {get;组; }
public virtual byte chance {get;组; }
public virtual MemberTheft action {get;组; }
...
它的映射:
public TheftMap()
{
表(thefts);
Id(x => x.id);
Map(x => x.name);
Map(x => x.id);
Map(x => x.chance);
引用(x => x.action)
.Nullable()
.PropertyRef(x => x.theftId)
.Column(id);
$ p $任何解决方案都可以做HQL,QueryOver等
使用LINQ提供程序不能完成,但是你可以使用QueryOver来完成。有些东西:
MemberTheft memberAlias = null;
var result = Session.QueryOver< Theft>()
.Left.JoinQueryOver(x => x.action,()=> memberAlias)
.Where((=)> ; memberAlias.memberId == member.id);
编辑:更新查询。
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
解决方案 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.
这篇关于Linq Nhibernate离开了加入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文