c# - Entity Framework Core 使用包含而不加载所有数据
问题描述
这是我的查询
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 进行查询并包装它,只需手动填充您的集合...
不是艺术品,但会起作用
推荐阅读
- java - Gluon-Mobile 应用程序没有声音
- sql-server - Visual Studio 2017 未显示 SSIS
- sql-server - 用于解析实例名称的端口号的 SQL Server Browser 服务的缓存在哪里?
- javascript - 如何使用 :not() 选择器,包括 parent:first-child 元素
- python - 是否可以使用 openpyxl 更改列宽?
- react-native - 是否可以通过单击任何按钮以编程方式更改 MapView Market pinColor?
- python - 带有元组键的字典:具有相同第一个元素的所有元组
- html - 可悬停下拉菜单中项目重叠的问题
- javascript - 弃用同步 XMLHttpRequest,纯 JavaScript
- android - Recyclerview 有时会在滚动时丢失数据