linq嵌套实体和集合
linq nested entities and collection
我有如下实体模型。
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);
我有如下实体模型。
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);