4
I have a LINQ query where I get the complete data. Because of performance, I am converting the query and using Dapper. I have the following entities:
public class Cliente
    {
        public int ClienteId { get; set; }
        public string Orgao { get; set; }
        public string NomeEntidade { get; set; }
        public string Responsavel { get; set; }
        public string Telefone { get; set; }
        public string Observacao { get; set; }
        public DateTime DataCadastro { get; set; }
        public bool Ativo { get; set; }
        public int EstadoId { get; set; }
        public int CidadeId { get; set; }
        public int FilialId { get; set; }
        public virtual Cidade Cidade { get; set; }
        public virtual Estado Estado { get; set; }
        public virtual Filial Filial { get; set; }
        public virtual ICollection<ClientePrestacao> ClientesPrestacoes { get; set; } 
    }
 public class ClientePrestacao
    {
        public int ClientePrestacaoId { get; set; }
        public int ClienteId { get; set; }
        public int TipoPrestacaoId { get; set; }
        public int EnvioPrestacaoId { get; set; }
        public virtual Cliente Cliente { get; set; }
        public virtual TipoPrestacao TipoPrestacao { get; set; }
        public virtual EnvioPrestacao EnvioPrestacao { get; set; }
    }
 public class EnvioPrestacao
    {
        public int EnvioPrestacaoId { get; set; }
        public bool bJaneiro { get; set; }
        public bool bFevereiro { get; set; }
        public bool bMarco { get; set; }
        public bool bAbril { get; set; }
        public bool bMaio { get; set; }
        public bool bJunho { get; set; }
        public bool bJulho { get; set; }
        public bool bAgosto { get; set; }
        public bool bSetembro { get; set; }
        public bool bOutubro { get; set; }
        public bool bNovembro { get; set; }
        public bool bDezembro { get; set; }
        public bool bTreze { get; set; }
        public bool bQuatorze { get; set; }
        public int? TipoPendencia { get; set; }
    }
   public class Estado
    {
        public int EstadoId { get; set; }
        public string Sigla { get; set; }
        public string Nome { get; set; }
    }
 public class Filial
    {
        public int FilialId { get; set; }
        public string Nome { get; set; }
        public int EstadoId { get; set; }
        public virtual Estado Estado { get; set; }
    }
    public class TipoPrestacao
    {
        public int TipoPrestacaoId { get; set; }
        public string Descricao { get; set; }
        public string TipoVencimento { get; set; }
        public string ResponsavelTCE { get; set; }
        public string ContatoTCE { get; set; }
        public int? QuantidadeArquivos { get; set; }
        public string AnoVigencia { get; set; }
        public DateTime DataCadastro { get; set; }
        public int VencimentoPrestacaoId { get; set; }
        public virtual VencimentoPrestacao VencimentoPrestacao { get; set; }
        public virtual ICollection<AnexoPrestacao> Anexos { get; set; }
        public virtual ICollection<ClientePrestacao> ClientesPrestacoes { get; set; } 
    }
public class VencimentoPrestacao
    {
        public int VencimentoPrestacaoId { get; set; }
        public DateTime? DataJaneiro { get; set; }
        public DateTime? DataFevereiro { get; set; }
        public DateTime? DataMarco { get; set; }
        public DateTime? DataAbril { get; set; }
        public DateTime? DataMaio { get; set; }
        public DateTime? DataJunho { get; set; }
        public DateTime? DataJulho { get; set; }
        public DateTime? DataAgosto { get; set; }
        public DateTime? DataSetembro { get; set; }
        public DateTime? DataOutubro { get; set; }
        public DateTime? DataNovembro { get; set; }
        public DateTime? DataDezembro { get; set; }
        public DateTime? DataCadastro { get; set; }
        public DateTime? DataTreze { get; set; }
        public DateTime? DataQuatorze { get; set; }
    }
And I’m using the following query to return the data:
select * from ClientePrestacao cp INNER JOIN clientes c on (cp.ClienteId = c.ClienteId)
                        INNER JOIN TipoPrestacao T ON (CP.TipoPrestacaoId = T.TipoPrestacaoId)
                        INNER JOIN EnvioPrestacao E ON (CP.EnvioPrestacaoId = e.EnvioPrestacaoId)
                        INNER JOIN VencimentoPrestacao v ON (t.VencimentoPrestacaoId = v.VencimentoPrestacaoId)
                        inner join Estados es on (es.EstadoId = c.EstadoId)
                        inner join Filiais f on(f.FilialId = c.FilialId)
                order by c.cidadeid, Orgao desc, c.nomeEntidade;
In the SQL query returns the correct values.
I’m using this code to consult with Dapper:
 public IEnumerable<ClientePrestacao> ObterTodos()
        {
            const string sql =
              @"select * from ClientePrestacao cp INNER JOIN clientes c on (cp.ClienteId = c.ClienteId)
                        INNER JOIN TipoPrestacao T ON (CP.TipoPrestacaoId = T.TipoPrestacaoId)
                        INNER JOIN EnvioPrestacao E ON (CP.EnvioPrestacaoId = e.EnvioPrestacaoId)
                        INNER JOIN VencimentoPrestacao v ON (t.VencimentoPrestacaoId = v.VencimentoPrestacaoId)
                        INNER JOIN Estados es on (es.EstadoId = c.EstadoId)
                        INNER JOIN Filiais f on(f.FilialId = c.FilialId)
                order by c.cidadeid, Orgao desc, c.nomeEntidade";
            using (var cn = Connection)
            {
                cn.Open();
                var clientes = cn.Query<ClientePrestacao, TipoPrestacao, VencimentoPrestacao, EnvioPrestacao, Filial, Estado, Cliente, ClientePrestacao>
                    (sql, (cp, t, v, e, f, es, c) =>
                    {
                        cp.TipoPrestacao = t;
                        cp.EnvioPrestacao = e;
                        cp.Cliente = c;
                        cp.TipoPrestacao.VencimentoPrestacao = v;
                        c.Filial = f;
                        c.Estado = es;
                        return cp;
                    }, splitOn: " ClientePrestacaoId, ClienteId, TipoPrestacaoId, EnvioPrestacaoId, VencimentoPrestacaoId,  FilialId, EstadoId");
                return clientes.ToList();
            }
        }
However, the entities Type Season, Expiration, Shipping and Customer are null.
Debugging, they really are null.
I would like to understand why some entities are null, and others not.
Edit
In tests, I noticed that if I change the query to:
 public IEnumerable<ClientePrestacao> ObterTodos()
        {
            const string sql =
              @"select * from ClientePrestacao cp INNER JOIN clientes c on (cp.ClienteId = c.ClienteId)
                order by c.cidadeid, Orgao desc, c.nomeEntidade";
            using (var cn = Connection)
            {
                cn.Open();
                var clientes = cn.Query<ClientePrestacao, Cliente, ClientePrestacao>
                    (sql, (cp,  c) =>
                    {
                        cp.Cliente = c;
                        return cp;
                    }, splitOn: " ClientePrestacaoId, ClienteId");
                return clientes.ToList();
            }
        }
Customers return the right values to me. However, when returning to the full query, it returns to me null.
Good question, managed to solve?
– Ewerton