C# 使用EF Core从多到多获取某些列
在多对多关系中,我只想从referencea表中获取某些列,如Id和Name,以填充selectlist。问题是,在使用Linq之前我还没有这样做过,我不知道什么是执行此过程的最佳方法 这是我的模型C# 使用EF Core从多到多获取某些列,c#,asp.net-core-2.2,entity-framework-core-2.2,C#,Asp.net Core 2.2,Entity Framework Core 2.2,在多对多关系中,我只想从referencea表中获取某些列,如Id和Name,以填充selectlist。问题是,在使用Linq之前我还没有这样做过,我不知道什么是执行此过程的最佳方法 这是我的模型 public class Celula { public int Id { get; set; } public string Nome { get; set; } public string UAP { get; set; } public ICollection&
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; }
}
现在有点乱,因为我不知道如何在这个查询中从referenceia表中选择某些列。我只需要Id和名称传递给lambda的
x
参数是一个referencea
实例,因此您只需执行以下操作:
.Select(x => new Referencia
{
Id = x.Id
Nome = x.Nome
});
谢谢,我把事情复杂化了,在导航属性上执行Selects和其他命令,而不是直接访问属性。顺便说一句,您的Include不需要,最后的select操作没有使用它们,where子句在数据库上运行。我知道了,我想我需要它们来连接所有数据。我想只有当我需要从它们中选择数据时才需要它们,谢谢你的提醒
.Select(x => new Referencia
{
Id = x.Id
Nome = x.Nome
});