簡體   English   中英

實體框架核心使用包括但不加載所有數據的情況

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM