Entity Framework Core - Include nested list inside list [duplicate]

≡放荡痞女 提交于 2019-12-13 20:18:26

问题


How can I include an inner list, which is inside another list, in a Entity Framework Core 2.0.1 query?

This is what I tried without success:

clase = repClase.ListQueryable(
    //Specification
    new ApplicationCore.Specifications.ClaseFilterByIdAndIdArticuloWithIncludesSpecification(idClase, idArticuloParam)
)
.Include(c => c.ReferenciasConstructor)
.ThenInclude(rc => rc.Select(rc1 => rc1.ReferenciaFabricanteTieneReferenciaConstructor))
.FirstOrDefault();

And the error it throws: "The property expression 'rc => {from ReferenciaConstructor rc1 in rc select [rc1].ReferenciaFabricanteTieneReferenciaConstructor}' is not valid. The expression should represent a property access: 't => t.MyProperty'.".

Classes:

[Table("Clases", Schema = "public")]
public class Clase
{

    ...

    [InverseProperty("Clase")]
    public IList<ReferenciaConstructor> ReferenciasConstructor { get; set; }

}

[Table("ReferenciasConstructor", Schema = "public")]
public class ReferenciaConstructor
{

    [Key]
    public int Id { get; set; }

    ...

    //JOIN TABLE
    [InverseProperty("ReferenciaConstructor")]
    public IList<ReferenciaFabricanteTieneReferenciaConstructor> ReferenciaFabricanteTieneReferenciaConstructor { get; set; }

}


//JOIN TABLE
[Table("ReferenciaFabricanteTieneReferenciaConstructor", Schema = "public")]
public class ReferenciaFabricanteTieneReferenciaConstructor {

    [Key]
    public int IdReferenciaFabricante { get; set; }

    [ForeignKey("IdReferenciaFabricante")]
    public ReferenciaFabricante ReferenciaFabricante { get; set; }

    [Key]
    public int IdReferenciaConstructor { get; set; }

    [ForeignKey("IdReferenciaConstructor")]
    public ReferenciaConstructor ReferenciaConstructor { get; set; }
}

回答1:


You can't use Include to select purpose. Just select the desired properties.

clase = repClase.ListQueryable(
        //Specification
        new ApplicationCore.Specifications.ClaseFilterByIdAndIdArticuloWithIncludesSpecification(idClase, idArticuloParam)
    )
    .Include(c => c.ReferenciasConstructor)
    .SelectMany(rc => rc.ReferenciasConstructor.Select(rc1 => rc1.ReferenciaFabricanteTieneReferenciaConstructor))
    .FirstOrDefault();


来源:https://stackoverflow.com/questions/48277087/entity-framework-core-include-nested-list-inside-list

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