error left Join Linq

Asked

Viewed 47 times

2

I cannot access the methods of a daughter class that was created using database first:

namespace Dados
{
    using System;
    using System.Collections.Generic;

    public partial class pessoa
    {
        public pessoa()
        {
            this.pessoa_endereco = new HashSet<pessoa_endereco>();
        }

        public int id { get; set; }
        public string tipo { get; set; }
        public string razao_social { get; set; }
        public string nome_fantasia { get; set; }
        public string cpf_cnpj { get; set; }
        public string rg_insc_estadual { get; set; }
        public string insc_substituicao { get; set; }
        public string insc_municipal { get; set; }
        public Nullable<System.DateTime> data_expedicao_rg { get; set; }
        public string orgao_expedidor_rg { get; set; }

        public virtual administrador administrador { get; set; }
        public virtual banco banco { get; set; }
        public virtual cliente cliente { get; set; }
        public virtual filial filial { get; set; }
        public virtual fornecedor fornecedor { get; set; }
        public virtual ICollection<pessoa_endereco> pessoa_endereco { get; set; }
        public virtual usuario usuario { get; set; }
        public virtual vendedor vendedor { get; set; }
    }
}

the daughter class is:

namespace Dados
{
    using System;
    using System.Collections.Generic;

    public partial class pessoa_endereco
    {
        public int id { get; set; }
        public int pessoa_id { get; set; }
        public string logradouro { get; set; }
        public string numero { get; set; }
        public string complemento { get; set; }
        public string ponto_referencia { get; set; }
        public string cep { get; set; }
        public string bairro { get; set; }
        public Nullable<int> cidade_id { get; set; }
        public string nome_contato { get; set; }
        public string telefone_1 { get; set; }
        public string ramal_telefone_1 { get; set; }
        public string telefone_2 { get; set; }
        public string ramal_telefone_2 { get; set; }
        public string celular { get; set; }
        public string email { get; set; }

        public virtual cidade cidade { get; set; }
        public virtual pessoa pessoa { get; set; }
    }
}

controller:

        takeeatEntities context = new takeeatEntities();

        var pessoas = context.pessoa
                     .GroupJoin(context.pessoa_endereco, p => p.id, a => a.pessoa_id, (p, a) => new { p, a })
                     .SelectMany(a => a.a.DefaultIfEmpty(), (p, a) => new PessoaDados
                     {
                         Id = p.p.id,
                         Razao_social = p.p.razao_social,
                         p.a. //não tem acesso aos metodos da pessoa_endereço
                     })
                     .ToList();

how can I access these methods in the controller, because when I put p.a. it does not bring any personal address field, if I do p. it brings me all fields only of the parent person class

1 answer

2


I didn’t quite understand your question more by looking at your code what it seems to me is that you didn’t do the correct instance in the child class, so then you can get your attributes so let’s see how it would look:

PARENT CLASS :

public partial class pessoa
{       
    public int id { get; set; }
    public string tipo { get; set; }
    public string razao_social { get; set; }
    public string nome_fantasia { get; set; }
    public string cpf_cnpj { get; set; }
    public string rg_insc_estadual { get; set; }
    public string insc_substituicao { get; set; }
    public string insc_municipal { get; set; }
    public Nullable<System.DateTime> data_expedicao_rg { get; set; }
    public string orgao_expedidor_rg { get; set; }

    public virtual administrador administrador { get; set; }
    public virtual banco banco { get; set; }
    public virtual cliente cliente { get; set; }
    public virtual filial filial { get; set; }
    public virtual fornecedor fornecedor { get; set; }          
    public virtual usuario usuario { get; set; }
    public virtual vendedor vendedor { get; set; }

}

DAUGHTER CLASS :

  public partial class pessoa_endereco : pessoa  // neste caso a classe filha herda os atributos da classe pai
        {
            public int id { get; set; }
            public int pessoa_id { get; set; }
            public string logradouro { get; set; }
            public string numero { get; set; }
            public string complemento { get; set; }
            public string ponto_referencia { get; set; }
            public string cep { get; set; }
            public string bairro { get; set; }
            public Nullable<int> cidade_id { get; set; }
            public string nome_contato { get; set; }
            public string telefone_1 { get; set; }
            public string ramal_telefone_1 { get; set; }
            public string telefone_2 { get; set; }
            public string ramal_telefone_2 { get; set; }
            public string celular { get; set; }
            public string email { get; set; }
            public virtual cidade cidade { get; set; }
            public virtual pessoa pessoa { get; set; }
        }

in this case with the daughter class you can have access to all attributes of the father that are public, so you do not need to repeat in the daughter class all that is common in the parent class. In the child class you only put the particular attributes of each person.

After that you can make the class that has access. I hope I have helped try to do so. Be careful as the code has not been tested look right to eliminate errors.

  • I still can not access the properties of the personal class_address, but I noticed by chance that I can access all the virtual public that were declared, then include it in the list and appeared, for sure I’m not doing the right way, but the problem now is another. I saw that database-first despite all the branch that breaks is not the solution idelal, because if I change a field in the bank and give an update on Model.edmx it will erase everything I did, although I think code-first will give me a lot of work because I will no longer be able to touch the bank directly to make changes, it is the best solution!

  • But I marked your answer as correct because it gave me a light on what to do, thank you very much for your help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.