如何使用LinqKit急切加载相关实体?

时间:2015-05-28 16:09:49

标签: c# linq entity-framework-4 code-first

我正在使用LinqKit来堆叠“Expression Predicates”,我必须对相关实体的Load进行“foreach”,这对于加载1000条记录的应用来说非常昂贵,平均需要4分钟。有没有办法加快这个过程加载?

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;
}

0 个答案:

没有答案