C# 如何获取父实体框架的子实体的子实体
我有三张桌子C# 如何获取父实体框架的子实体的子实体,c#,entity-framework,C#,Entity Framework,我有三张桌子 Inventarios -> Localizacoes -> Etiquetas 从左到右都有一对多关系 问题是我似乎无法到达Etiquetas // GET: api/Inventarios public IQueryable<Inventario> GetInventarios() { var inventarios = db.Inventarios.Include(i => i.Localizacoes.Select(l =>
Inventarios -> Localizacoes -> Etiquetas
从左到右都有一对多关系
问题是我似乎无法到达Etiquetas
// GET: api/Inventarios
public IQueryable<Inventario> GetInventarios()
{
var inventarios = db.Inventarios.Include(i => i.Localizacoes.Select(l =>
l.Etiquetas.SelectMany(e => e.Numero)));
return inventarios;
}
//GET:api/Inventarios
公共IQueryable GetInventarios()
{
var inventarios=db.inventarios.Include(i=>i.Localizacoes.Select(l=>
l、 Etiquetas.SelectMany(e=>e.Numero));
返还发明人;
}
这是模型
public class Inventario
{
public int InventarioID { get; set; }
public string Colaborador { get; set; }
public string Armazem { get; set; }
public decimal Total { get; set; }
public DateTime Data { get; set; }
public ICollection<Localizacao> Localizacoes { get; set; }
}
public class Localizacao
{
public int LocalizacaoID { get; set; }
public string Referencia { get; set; }
public int EtiquetasPorInventariar { get; set; }
public int EtiquetasInventariadas { get; set; }
public bool IsValid { get; set; }
public decimal Precisao { get; set; }
public int InventarioID { get; set; }
public Inventario Inventario { get; set; }
public ICollection<Etiqueta> Etiquetas{ get; set; }
}
public class Etiqueta
{
public int EtiquetaID { get; set; }
public string Numero { get; set; }
public int LocalizacaoID { get; set; }
public Localizacao Localizacao { get; set; }
}
公共类发明
{
public int InventarioID{get;set;}
公共字符串Colaborador{get;set;}
公共字符串Armazem{get;set;}
公共十进制总数{get;set;}
公共日期时间数据{get;set;}
公共ICollection Localizacoes{get;set;}
}
公共类本地化
{
公共int LocalizaoId{get;set;}
公共字符串引用{get;set;}
公共int EtiquetasPorInventariar{get;set;}
公共int-EtiquetasInventariadas{get;set;}
公共bool有效{get;set;}
公共十进制精度{get;set;}
public int InventarioID{get;set;}
公共发明{get;set;}
公共ICollection Etiquetas{get;set;}
}
公共类Etiqueta
{
公共int-EtiquetaID{get;set;}
公共字符串Numero{get;set;}
公共int LocalizaoId{get;set;}
公共本地化{get;set;}
}
这是我在浏览器控制台上从api请求得到的例外
“包含路径表达式必须引用导航属性
在类型上定义。使用虚线路径进行参考导航
属性和集合导航的选择运算符
财产。”
我的假设是您正在使用
IQueryable
来支持OData
除此之外,我在这里看到的两件事情看起来并不完全正确
第一个是SelectMany
,它可能应该是Select
var inventarios = db.Inventarios.Include(i => i.Localizacoes.Select(l =>
l.Etiquetas.Select(e => e.Numero)));
第二,您正在返回IQueryable
,我不确定这是否会遍历对象图
作为测试,你可以做这样的事情
return inventarios.ToList().AsQuerable();
SelectMany
看起来不正确,而且您返回的IQueryable
也令人怀疑,为什么?我想它是由Controller类生成的,我想不起来了,虽然你的问题不清楚你到底想得到什么?来自Etiqueta的所有行?您没有任何where
子句。是的,它与select一起工作,但我仍然是使用linq的初学者。但无论哪种方式,它都会以正确的格式返回json数据,并显示在一个包含2个嵌套表的表中