How to improve the loading of related entities using LinqKit?

1

Hi, I'm using LinqKit to stack "Expression Predicates", so I need to do a "foreach" to do the entities Load related and this is being very costly for application because to load 1000 records is taking in 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;
    }
    
asked by anonymous 28.05.2015 / 05:49

0 answers