c# - 使用 EF Core 从多到多获取某些列
问题描述
在多对多关系中,我只想从参考表中获取某些列,例如 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();
现在有点乱,因为我不知道如何在此查询中从参考表中选择某些列。我只需要 Id 和 Name
解决方案
x
您传递给 lambda的参数是一个Referencia
实例,因此您只需执行以下操作:
.Select(x => new Referencia
{
Id = x.Id
Nome = x.Nome
});
推荐阅读
- postgresql - Postgres 时间戳转换是向后的
- amazon-connect - Amazon Connect 联系流 CLI/API
- css - 即使安装了所有东西,如何解决引导程序不起作用的问题?
- python-3.x - 词形还原嵌套列表中的单词
- android - 伙计们,我正在用颤振编码一些东西,我从变量中取 null 你能帮我吗
- javascript - 车道分配算法
- html - 正确的结构未在 Html Display Flex 上显示
- javascript - 基于技能指数从 10 人列表中组成两个团队的算法
- bash - 将 xargs 与 cat 和 grep 结合使用
- node.js - 如何将 API 调用响应存储为对象或将数组解析为 JSON?