问题
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