Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!
I'm using EF and I have two classes: HRecibos and Clientes:
C#
[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; }  
    }

And I've a DBContext:
C#
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; }

I want to read all the "Recibos("Receipts") and put the client's name after the Cliente Code.
I wrote this:
C#
public DataTable GetAll()
{
    using (var context = new CAPDBContext())
    {
        IList<hrecibo> HRecibos = context.Recibos.ToList();
        IList<cliente> Clientes = context.Clientes.ToList();

        return HRecibos.ToDataTable<hrecibo>();
    }
}

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
Posted
Updated 12-Dec-14 5:47am
v2
Comments
Afzaal Ahmad Zeeshan 12-Dec-14 11:58am    
You want to get the Rebicos property?

1 solution

The simplest option would be to use the navigation property to read the necessary information:
C#
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:
C#
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:
C#
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();
    }
}
 
Share this answer
 
Comments
Antonio Barros 12-Dec-14 12:48pm    
Thank you very much. It worked fine. It was wonderful, because I've wasted hours looking for a solution. It's wonderful to kave such knowledge. It's very dificul to a newbie and it takes to much time to get there.
Thanks again
A.Barros
Maciej Los 12-Dec-14 12:54pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900