我有和实体模型如下。
public class reg{
public int id {get; set;}
...
public ICollection<trans> trans{get; set;}
}
public class trans{
public int id {get; set;}
...
public modifiedTrans modifiedTrans{get; set;}
public virtual reg reg {get; set;}
}
public class modifiedTrans {
public int id {get; set;}
...
public virtual trans trans {get; set;}
}
我需要一个linq表达式,它使我得到的结果由reg id过滤为
{
id : x
...
trans : {
id : y1,
...,
modifiedTrans : {
id : z1
...
}
},
{
id : y2,
...,
modifiedTrans : {
id : z2
...
}
}
}
到目前为止,我最终是这样的:
var query = from r in reg
let t = r.trans
let m = t.modifiedTrans
where (r => r.Id = 1)
select new
{
r = r,
t = t,
m = m
}.FirstOrDefault();
这不是我真正想要的。谁能帮忙!
这是一个棘手的答案,因为您有一个递归嵌套的实体。根据对象深层嵌套的频率以及对象的数量或正在加载的修改的百分比,可以更好地延迟或急于加载(或组合使用)嵌套的对象。
更新对不起,我误解你的类。
var result=_db.regs
.Include(r=>r.trans)
.Include(r=>r.trans.Select(t=>t.modifiedTrans))
.Where(r=>r.id==1);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句