如何在IList类型中访问IList类型中的项目?
嗨!
我正在使用EF而且我有两个类:HRecibos和Clientes:
Hi!
I'm using EF and I have two classes: HRecibos and Clientes:
[Table("Clientes")]
public class Cliente
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public int CodCliente { get; set; }
public string TitHonorif { get; set; }
public string Nome { get; set; }
public string Morada { get; set; }
public string CodPostal { get; set; }
public string Localidade { get; set; }
public string NrContrib { get; set; }
public string Obs { get; set; }
public decimal Valor { get; set; }
public byte Status { get; set; }
public IEnumerable<hrecibo>; Recibos { get; set; }
}
[Table("HRecibos")]
public class HRecibo
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public string NrRecibo { get; set; }
public int CodCliente { get; set; }
public Cliente Cliente { get; set; }
public decimal Valor { get; set; }
public byte Status { get; set; }
}
我有一个DBContext:
And I've a DBContext:
public partial class CAPDBContext : DbContext
{
public CAPDBContext()
: base("CAPDBContext")
{
Database.SetInitializer<CAPDBContext>( null );
}
public DbSet<hrecibo>; Recibos { get; set; }
public DbSet<cliente>; Clientes { get; set; }
我想阅读所有Recibos(收据)并将客户名称放在Cliente Code之后。
我写了这个:
I want to read all the "Recibos("Receipts") and put the client's name after the Cliente Code.
I wrote this:
public DataTable GetAll()
{
using (var context = new CAPDBContext())
{
IList<hrecibo> HRecibos = context.Recibos.ToList();
IList<cliente> Clientes = context.Clientes.ToList();
return HRecibos.ToDataTable<hrecibo>();
}
}
所以我的IList类型Hrecibos包含所有Recibos(收据)和其中的另一个Ilist Cliente。
问题是:我如何得到客户端的名称包含客户数据的内幕IList?
提前谢谢
$ b $bAntónioBarros
So I've the IList type "Hrecibos" with all "Recibos" (Receipts) and another Ilist Cliente inside it.
The problem is: How I get the name of the client in the insider IList that contains the clients data?
Thank you in advance
António Barros
最简单的选择是使用navigation属性来读取必要的信息:
The simplest option would be to use the navigation property to read the necessary information:
public IList<HRecibo> GetAll()
{
using (var context = new CAPDBContext())
{
return context.Recibos
.Include(r => r.Cliente)
.ToList();
}
}
...
foreach (HRecibo item in GetAll())
{
Console.WriteLine("{0}, {1}, {2}", item.NrRecibo, item.CodCliente, item.Cliente.Nome);
}
如果你需要压扁列表,你可以使用匿名类型:
If you need to flatten the list, you can either use an anonymous type:
public IList<dynamic> GetAll()
{
using (var context = new CAPDBContext())
{
return context.Recibos
.Include(r => r.Cliente)
.Select(r => new
{
r.NrRecibo,
r.CodCliente,
Nome = r.Cliente.Nome,
r.Valor,
r.Status,
})
.ToList();
}
}
或者您可以为数据创建特定类型:
Or you can create a specific type for the data:
public sealed class HReciboDto
{
public string NrRecibo { get; set; }
public int CodCliente { get; set; }
public string Nome { get; set; }
public decimal Valor { get; set; }
public byte Status { get; set; }
}
public IList<HReciboDto> GetAll()
{
using (var context = new CAPDBContext())
{
return context.Recibos
.Include(r => r.Cliente)
.Select(r => new HReciboDto
{
NrRecibo = r.NrRecibo,
CodCliente = r.CodCliente,
Nome = r.Cliente.Nome,
Valor = r.Valor,
Status = r.Status,
})
.ToList();
}
}