C# 如何在实体框架中列出连接实体属性而不是完整实体?
我在一个名为Computer和Department的SQL数据库中有两个表,它们通过一个名为DepartmentComputer的表(PK是ComputerId)具有1对多的关系 我试图在DataGridView上列出所有具有各自部门名称的计算机,但当涉及DataGridView上的部门时,实际上会显示实体,而不是部门名称,如下所示: 以下是从通用存储库获取所有计算机所需的方法:C# 如何在实体框架中列出连接实体属性而不是完整实体?,c#,entity-framework,datagridview,C#,Entity Framework,Datagridview,我在一个名为Computer和Department的SQL数据库中有两个表,它们通过一个名为DepartmentComputer的表(PK是ComputerId)具有1对多的关系 我试图在DataGridView上列出所有具有各自部门名称的计算机,但当涉及DataGridView上的部门时,实际上会显示实体,而不是部门名称,如下所示: 以下是从通用存储库获取所有计算机所需的方法: public Task<List<TEntity>> GetAllAsync()
public Task<List<TEntity>> GetAllAsync()
{
return Context.Set<TEntity>().ToListAsync();
}
public async Task<IEnumerable<Computadora>> ListarAsync()
{
return await _unitOfWork.Computadora.GetAllAsync();
}
公共任务GetAllAsync()
{
返回Context.Set().toListSync();
}
公共异步任务ListarAsync()
{
return wait_unitOfWork.Computadora.GetAllAsync();
}
这些是填充DataGridView的方法
private async void ListarComputadoras()
{
var lista = await ListarAsync();
Popular(lista);
}
public void Popular(IEnumerable<Computadora> computadoras)
{
var bs = new BindingSource() {DataSource = computadoras.ToList()};
dgvDatos.DataSource = bs;
}
private async void ListarComputadoras()
{
var lista=等待ListarAsync();
流行的(lista);
}
公共空间(IEnumerable computadoras)
{
var bs=new BindingSource(){DataSource=computadoras.ToList()};
dgvDatos.DataSource=bs;
}
如何选择表Department的属性名称并在DataGridView上显示它,而不是显示实体的名称
谢谢大家!
编辑:我忘了提。我想避免使用匿名类型,因为我有更多依赖于计算机列表的逻辑,并且匿名类型会破坏逻辑。试试这个
public void Popular(IEnumerable<Computadora> computadoras)
{
var data = (from c in computadoras
select new {
CodigoInterno = c.CodigoInterno,
Departamento = c.Departamento.Name, // If the department entity has a name property.
// assign the properties to be shown in grid like this
}).ToList();
var bs = new BindingSource() {DataSource = data};
dgvDatos.DataSource = bs;
}
public void Popular(IEnumerable computadoras)
{
变量数据=(来自计算机中的c)
选择新的{
CodigoInterno=c.CodigoInterno,
departmento=c.departmento.Name,//如果部门实体具有Name属性。
//指定要在网格中显示的属性,如下所示
}).ToList();
var bs=new BindingSource(){DataSource=data};
dgvDatos.DataSource=bs;
}
如果您不喜欢使用匿名类型,请创建一个具有所需属性的类,并按如下方式使用它
public void Popular(IEnumerable<Computadora> computadoras)
{
var data = (from c in computadoras
select new Computer{
CodigoInterno = c.CodigoInterno,
Departamento = c.Departamento.Name, //If the department entity has a name property.
// assign the properties to be shown in grid like this
}).ToList();
var bs = new BindingSource() {DataSource = data};
dgvDatos.DataSource = bs;
}
public class Computer
{
public string CodigoInterno {get;set;}
public string Departamento {get;set;}
//add properties here
}
public void Popular(IEnumerable computadoras)
{
变量数据=(来自计算机中的c)
选择新计算机{
CodigoInterno=c.CodigoInterno,
departmento=c.departmento.Name,//如果部门实体具有Name属性。
//指定要在网格中显示的属性,如下所示
}).ToList();
var bs=new BindingSource(){DataSource=data};
dgvDatos.DataSource=bs;
}
公共类计算机
{
公共字符串CodigoInterno{get;set;}
公共字符串departmento{get;set;}
//在此处添加属性
}
我试过了,效果不错,但是可以避免使用匿名类型吗?因为我有更多的逻辑依赖于计算机列表,并且匿名类型会破坏逻辑。如果你不喜欢使用匿名类型,请为此创建一个类并使用它。查看更新的答案