How to improve the loading of related entities using Linqkit?

Asked

Viewed 64 times

1

Hi, I’m using Linqkit To stack "Expression Predicates", I need to do a "foreach" to load the related entities and this is being very expensive for application because to load 1000 records is taking on average 4 minutes. Is there any way to streamline this Load process?

 public override IQueryable<Entity.Modelos.FluxoDeCaixa> Filtro(System.Linq.Expressions.Expression<System.Func<Entity.Modelos.FluxoDeCaixa, bool>> expressao)
    {

        var query = from f in _contexto.FluxoDeCaixa.AsExpandable().Where(expressao)                        
                    from fu in _contexto.Funcionario.Where(x => x.PessoaId == f.FuncionarioId)
                    from pl in _contexto.PlanoDeContas.Where(x => x.PlanoDeContasId == f.PlanoDeContasId)
                    from pf in _contexto.PessoaFisica.Where(x => x.PessoaId == f.PessoaId).DefaultIfEmpty()
                    from pj in _contexto.PessoaJuridica.Where(x => x.PessoaId == f.PessoaId).DefaultIfEmpty()                                                
                    select f;

        foreach (var f in query)
        {
            _contexto.Entry(f)
            .Reference(r => r.PlanoDeContas)
            .Load();

            _contexto.Entry(f)
                .Reference(r => r.Funcionario)
                .Load();

            _contexto.Entry(f)
                .Reference(r => r.Pessoa)
                .Load();
        }


        return query;
    }
No answers

Browser other questions tagged

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