Problem with Linq in Lambda: LINQ to Entities does not recognize... get_Item(Int32). How to solve?



  • When trying to perform the loop for (moment in which query is performs) is making error in my lambda expression:

    var funcionarios = repository.Funcionarios
        .Include(x => x.Cargo)
        .Include(x => x.Bairro)
        .Include(x => x.Bairro.Cidade)
        .Include(x => x.Historicos)
        .Where(x => x.Historicos
            .Count(hist => hist.TipoHistoricoId == parametros.HistoricoManutencaoCargoId &&
                hist.DataRegistro.Year == AnoBase) > 0)
        .Where(x => x.Bairro != null && x.Bairro.Cidade != null)
        .OrderBy(x => x.Bairro.Cidade.Id)
        .ThenByDescending(x => x.Historicos[0].DataRegistro);
    

    foreach (var func in funcionarios)
    {
    ...
    }

    I am getting the following error:

    LINQ to Entities does not recognize the method 'Domain.FunctionioHistorico get_Item(Int32)' method, and this method cannot be translated into a store expression.

    Domain.FuncionarioHistorico refers to the property Historicos of the Employee being loaded by: .Include(x => x.Historicos).

    Apparently my expression for Join / Include is not correct, or need to store any of the values used in it to then pass to the expression.

    O parametros is a local variable that stores some system parameters, including the History Type that is used to maintain positions. HistoricoManutencaoCargoId is the property that accesses the Id like that.

    There is a need for the model I add a small example because the current is great.

    How to solve this?



  • O Include waits for an object complex related to your Model: http://msdn.microsoft.com/en-us/library/gg696785(v=vs.113).aspx

    So what causes the problem is not Include, and yes Count, that within one Where cannot be translated immediately. So the mistake.

    Anyway, that way it's written is weird. You can simplify to:

    var funcionarios = repository.Funcionarios
        .Include(x => x.Cargo)
        .Include(x => x.Bairro)
        .Include(x => x.Bairro.Cidade)
        .Include(x => x.Historicos)
        .Where(x => x.Bairro != null && x.Bairro.Cidade != null)
        .OrderBy(x => x.Bairro.Cidade.Id)
        .ThenByDescending(x => x.Historicos[0].DataRegistro);
    

    funcionarios = funcionarios.SelectMany(f => f.Historicos)
    .Where(hist => hist.TipoHistoricoId == parametros.HistoricoManutencaoCargoId &&
    hist.DataRegistro.Year == AnoBase)
    .Select(hist => hist.Funcionario)
    .ToList();




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2