[英]Entity Framework Core use take on include without loading all data
这是我的查询
var geometrias = _context.Geometrias
.Include(g => g.GeometriaRegistos)
.Include(g => g.Matriz)
.ThenInclude(m => m.Referencia)
.Where(g => g.Matriz.ReferenciaId == referenciaId && !g.IsDeleted && !g.Matriz.IsDeleted)
.OrderByDescending(g => g.Id)
.Take(20)
.ToList();
我只想每个Geometria获得20个GeometriaRegistos
但
.Include(g => g.GeometriaRegistos.Take(20))
不起作用
这是模特
public class GeometriaRegisto
{
public int Id { get; set; }
public float X1 { get; set; }
public float X2 { get; set; }
public float X3 { get; set; }
public float X4 { get; set; }
public int GeometriaId { get; set; }
public Geometria Geometria { get; set; }
public int ProducaoRegistoId { get; set; }
public ProducaoRegisto ProducaoRegisto { get; set; }
}
public class Geometria
{
public int Id { get; set; }
public string Componente { get; set; }
public bool IsDeleted { get; set; }
public int MatrizId { get; set; }
public Matriz Matriz { get; set; }
public ICollection<GeometriaRegisto> GeometriaRegistos { get; set; }
}
我曾经用Dapper和SQL存储过程来做到这一点,但我试图使用linq来管理它,但是我找不到一种方法,而不必先将所有它们都加载到内存中,然后再次进行过滤,但是这样做很多数据
不幸的是,不支持这种情况。
当您.Include
您实际上是在发出您希望JOIN
该关系的信号。
为了实现你的愿望,我建议你获取你所有Geometrias
而不包括GeometriasRegistros
。 然后选择您要获取的id,例如grIds = geometrias.Select(g=>g.GeometriasRegistros.Take(20).Id).Distinct()
。 下一步是根据这些ID进行查询并进行包装,只需手动填充您的收藏夹即可。
不是艺术品,但会起作用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.