2
I have the representation of 2 classes.
first class:
using System;
using System.Runtime.Serialization;
namespace WebService
{
[DataContract]
public class PessoaDados
{
private Nullable<int> id;
private string tipo;
private string razao_social;
private string nome_fantasia;
private string cpf_cnpj;
private string rg_insc_estadual;
private string insc_substituicao;
private string insc_municipal;
private DateTime? data_expedicao_rg;
private string orgao_expedidor_rg;
[DataMember]
public int? Id { get => id; set => id = value; }
[DataMember]
public string Tipo { get => tipo; set => tipo = value; }
[DataMember]
public string Razao_social { get => razao_social; set => razao_social = value; }
[DataMember]
public string Nome_fantasia { get => nome_fantasia; set => nome_fantasia = value; }
[DataMember]
public string Cpf_cnpj { get => cpf_cnpj; set => cpf_cnpj = value; }
[DataMember]
public string Rg_insc_estadual { get => rg_insc_estadual; set => rg_insc_estadual = value; }
[DataMember]
public string Insc_substituicao { get => insc_substituicao; set => insc_substituicao = value; }
[DataMember]
public string Insc_municipal { get => insc_municipal; set => insc_municipal = value; }
[DataMember]
public DateTime? Data_expedicao_rg { get => data_expedicao_rg; set => data_expedicao_rg = value; }
[DataMember]
public string Orgao_expedidor_rg { get => orgao_expedidor_rg; set => orgao_expedidor_rg = value; }
}
}
2nd class
using System;
using System.Runtime.Serialization;
namespace WebService
{
[DataContract]
public class Pessoa_enderecoDados
{
private Nullable<int> id;
private int pessoa_id;
private string logradouro;
private int numero;
private string complemento;
private string ponto_referencia;
private string cep;
private string bairro;
private int cidade_id;
private string nome_contato;
private string telefone_1;
private string ramal_telefone_1;
private string telefone_2;
private string ramal_telefone_2;
private string celular;
private string email;
[DataMember]
public int? Id { get => id; set => id = value; }
[DataMember]
public int Pessoa_id { get => pessoa_id; set => pessoa_id = value; }
[DataMember]
public string Logradouro { get => logradouro; set => logradouro = value; }
[DataMember]
public int Numero { get => numero; set => numero = value; }
[DataMember]
public string Complemento { get => complemento; set => complemento = value; }
[DataMember]
public string Ponto_referencia { get => ponto_referencia; set => ponto_referencia = value; }
[DataMember]
public string Cep { get => cep; set => cep = value; }
[DataMember]
public string Bairro { get => bairro; set => bairro = value; }
[DataMember]
public int Cidade_id { get => cidade_id; set => cidade_id = value; }
[DataMember]
public string Nome_contato { get => nome_contato; set => nome_contato = value; }
[DataMember]
public string Telefone_1 { get => telefone_1; set => telefone_1 = value; }
[DataMember]
public string Ramal_telefone_1 { get => ramal_telefone_1; set => ramal_telefone_1 = value; }
[DataMember]
public string Telefone_2 { get => telefone_2; set => telefone_2 = value; }
[DataMember]
public string Ramal_telefone_2 { get => ramal_telefone_2; set => ramal_telefone_2 = value; }
[DataMember]
public string Celular { get => celular; set => celular = value; }
[DataMember]
public string Email { get => email; set => email = value; }
}
}
I need to make a Join of person with personal addressee using LINQ. I tried to do it this way but it’s not working yet:
public IList<PessoaDados> Select()
{
takeeatEntities context = new takeeatEntities();
IList<PessoaDados> listaPessoasEntity = new List<PessoaDados>();
IList<pessoa> pessoas = (from p in context.pessoa
join e in context.pessoa_endereco on e.pessoa_id equals p.id ed
from d in ed.DefaultIfEmpty()
select new { * });
PessoaDados pessoa_dados = null;
foreach (pessoa Pessoa in pessoas)
{
pessoa_dados = new PessoaDados();
pessoa_dados.Tipo = Pessoa.tipo;
pessoa_dados.Razao_social = Pessoa.razao_social;
pessoa_dados.Nome_fantasia = Pessoa.nome_fantasia;
pessoa_dados.Cpf_cnpj = Pessoa.cpf_cnpj;
pessoa_dados.Rg_insc_estadual = Pessoa.rg_insc_estadual;
pessoa_dados.Insc_substituicao = Pessoa.insc_substituicao;
pessoa_dados.Insc_municipal = Pessoa.insc_municipal;
pessoa_dados.Data_expedicao_rg = Pessoa.data_expedicao_rg;
pessoa_dados.Orgao_expedidor_rg = Pessoa.orgao_expedidor_rg;
listaPessoasEntity.Add(pessoa_dados);
}
return listaPessoasEntity;
}
I’m still a beginner in LINQ, Lambda.
Below follows the error image, which does not even compile:

using System;
using System.Runtime.Serialization;
namespace WebService
{
[DataContract]
public class PessoaDados
{
private Nullable<int> id;
private string tipo;
private string razao_social;
private string nome_fantasia;
private string cpf_cnpj;
private string rg_insc_estadual;
private string insc_substituicao;
private string insc_municipal;
private DateTime? data_expedicao_rg;
private string orgao_expedidor_rg;
[DataMember]
public int? Id { get => id; set => id = value; }
[DataMember]
public string Tipo { get => tipo; set => tipo = value; }
[DataMember]
public string Razao_social { get => razao_social; set => razao_social = value; }
[DataMember]
public string Nome_fantasia { get => nome_fantasia; set => nome_fantasia = value; }
[DataMember]
public string Cpf_cnpj { get => cpf_cnpj; set => cpf_cnpj = value; }
[DataMember]
public string Rg_insc_estadual { get => rg_insc_estadual; set => rg_insc_estadual = value; }
[DataMember]
public string Insc_substituicao { get => insc_substituicao; set => insc_substituicao = value; }
[DataMember]
public string Insc_municipal { get => insc_municipal; set => insc_municipal = value; }
[DataMember]
public DateTime? Data_expedicao_rg { get => data_expedicao_rg; set => data_expedicao_rg = value; }
[DataMember]
public string Orgao_expedidor_rg { get => orgao_expedidor_rg; set => orgao_expedidor_rg = value; }
public virtual Pessoa_enderecoDados pessoa_enderecoDados { get; set; }
}
[DataContract]
public class Pessoa_enderecoDados
{
private Nullable<int> id;
private int pessoa_id;
private string logradouro;
private int numero;
private string complemento;
private string ponto_referencia;
private string cep;
private string bairro;
private int cidade_id;
private string nome_contato;
private string telefone_1;
private string ramal_telefone_1;
private string telefone_2;
private string ramal_telefone_2;
private string celular;
private string email;
[DataMember]
public int? Id { get => id; set => id = value; }
[DataMember]
public int Pessoa_id { get => pessoa_id; set => pessoa_id = value; }
[DataMember]
public string Logradouro { get => logradouro; set => logradouro = value; }
[DataMember]
public int Numero { get => numero; set => numero = value; }
[DataMember]
public string Complemento { get => complemento; set => complemento = value; }
[DataMember]
public string Ponto_referencia { get => ponto_referencia; set => ponto_referencia = value; }
[DataMember]
public string Cep { get => cep; set => cep = value; }
[DataMember]
public string Bairro { get => bairro; set => bairro = value; }
[DataMember]
public int Cidade_id { get => cidade_id; set => cidade_id = value; }
[DataMember]
public string Nome_contato { get => nome_contato; set => nome_contato = value; }
[DataMember]
public string Telefone_1 { get => telefone_1; set => telefone_1 = value; }
[DataMember]
public string Ramal_telefone_1 { get => ramal_telefone_1; set => ramal_telefone_1 = value; }
[DataMember]
public string Telefone_2 { get => telefone_2; set => telefone_2 = value; }
[DataMember]
public string Ramal_telefone_2 { get => ramal_telefone_2; set => ramal_telefone_2 = value; }
[DataMember]
public string Celular { get => celular; set => celular = value; }
[DataMember]
public string Email { get => email; set => email = value; }
}
}
consultation:
public IList<PessoaDados> Select()
{
takeeatEntities context = new takeeatEntities();
IList<PessoaDados> listaPessoasEntity = new List<PessoaDados>();
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.a.razao_social
})
.ToList();
PessoaDados pessoa_dados = null;
foreach (pessoa Pessoa in pessoas)
{
pessoa_dados = new PessoaDados();
pessoa_dados.Tipo = Pessoa.tipo;
pessoa_dados.Razao_social = Pessoa.razao_social;
pessoa_dados.Nome_fantasia = Pessoa.nome_fantasia;
pessoa_dados.Cpf_cnpj = Pessoa.cpf_cnpj;
pessoa_dados.Rg_insc_estadual = Pessoa.rg_insc_estadual;
pessoa_dados.Insc_substituicao = Pessoa.insc_substituicao;
pessoa_dados.Insc_municipal = Pessoa.insc_municipal;
pessoa_dados.Data_expedicao_rg = Pessoa.data_expedicao_rg;
pessoa_dados.Orgao_expedidor_rg = Pessoa.orgao_expedidor_rg;
pessoa_dados.
listaPessoasEntity.Add(pessoa_dados);
}
return listaPessoasEntity;
}
What is the error obtained?
– mercador
You are using
Entity Frameworkfor data load? Entities are related?– novic
put an image with the error, yes it is Entity framework. model.edmx with the tables loaded. they have relationships with each other, my intention is to select and feed these 2 classes, or unite them, I just have no idea how I do it, I added an image of the error
– Gleyson Silva
In the case exists
pessoathat does not havepessoa_enderecoand you want to bring all the people?– novic
That’s right, I need it like this
– Gleyson Silva
@Intelidersistemas from what I understand, is mounting a
DTOand marking withDataContract, is sending this data to a serviceWCFor riding aViewModelto theMVC?– Tobias Mesquita
I am sending to a WCF, when I do with only one table works ok
– Gleyson Silva