简体   繁体   中英

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();

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM