Leandro de Souza
Leandro de Souza

Reputation: 33

Entity Framework - Related Entity Perfomance

i have an query to load related entity for an specific entity, but it´s take a 5 seconds to load data, what happened?

var rows = clientes.Select(c => new
        {
            c.Id,
            c.Nome,
            Telefone = String.Format("(0{0}) {1}", c.DDD, c.Telefone),
            c.Email,
            Veiculo = (from v in c.Veiculos select new { v.Id, v.Modelo, v.Chassi }),
        })
    .Skip(pageNumber > 1 ? qtdRows * (pageNumber - 1) : 0)
    .Take(qtdRows)
    .ToArray();

Upvotes: 1

Views: 87

Answers (1)

Kirill Bestemyanov
Kirill Bestemyanov

Reputation: 11964

It seems that you join two entities but not using filter to get Veiculos of current Clientes.

May be you should use something like

var rows = clientes.Select(c => new
        {
            c.Id,
            c.Nome,
            Telefone = String.Format("(0{0}) {1}", c.DDD, c.Telefone),
            c.Email,
            Veiculo = (from v in c.Veiculos *where v.ClientId == c.Id* select new { v.Id, v.Modelo, v.Chassi }),
        })
    .Skip(pageNumber > 1 ? qtdRows * (pageNumber - 1) : 0)
    .Take(qtdRows)
    .ToArray();

But, more consistent method is to add navigation property Veiculo to entity Client and lay joining of tables on Entity Framework.

Upvotes: 2

Related Questions