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();
}
}