[英]Get certain columns from many to many using EF Core
在多對多關系中,我只想從Referencia表中獲取某些列(例如Id和Name)以填充選擇列表。 問題是在使用Linq之前我還沒有做過此事,而且我不知道執行此過程的最佳方法是什么。
這是我的模特。
public class Celula
{
public int Id { get; set; }
public string Nome { get; set; }
public string UAP { get; set; }
public ICollection<MatrizCelulaReferencia> Matrizes { get; set; }
}
public class Referencia
{
public int Id { get; set; }
public string Nome { get; set; }
public bool IsActivo { get; set; }
public ICollection<MatrizCelulaReferencia> Matrizes { get; set; }
}
public class MatrizCelulaReferencia
{
public int Id { get; set; }
public int CelulaId { get; set; }
public Celula Celula { get; set; }
public int ReferenciaId { get; set; }
public Referencia Referencia { get; set; }
public int ObjectivoHora { get; set; }
public int UAPId { get; set; }
public UAP UAP { get; set; }
public string Tipo { get; set; }
}
這是我當前的查詢
var query = await _context.Referencias
.Include(r => r.Matrizes)
.ThenInclude(r => r.Celula)
.Where(r => r.Matrizes.Any(c => c.CelulaId == Operador.Celula))
.Select(x => new Referencia
{
Id = // Referencia Id
Nome = // Referencia Name
})
.ToListAsync();
現在這有點混亂,因為我不知道如何從該查詢的Referencia表中選擇某些列。 我只需要ID和名字
您傳遞給lambda的x
參數是Referencia
實例,因此您只需執行以下操作:
.Select(x => new Referencia
{
Id = x.Id
Nome = x.Nome
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.