Home > Net >  Error with Linq query especific ArrayIndex
Error with Linq query especific ArrayIndex

Time:07-03

I have one issue with this query in linq and I dont see anything wear, anyone can help me with that!

The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities

 public List<FiltroItem> CargarFiltroValidacionInformacion(FiltroViewModel filtro, bool validarUsuario, List<decimal> estadosValidos)
    {
        var query = from ce in _contexto.Certificacion_MPP
                    where ce.CODENTIDAD.ToString() == Motor.Certificado.Entidad
                    select ce;

        if (validarUsuario)
            query = query.Where(x => x.USUARIOASIGNADO.Contains(Motor.Certificado.Usuario));

        if (filtro.CodEmpleado.HasValue)
            query = query.Where(x => x.CODEMPLEADO == filtro.CodEmpleado);

        if (estadosValidos.Any())
            query = query.Where(x => x.CODESTADOCERTIFICACION.ToString().Any(y => estadosValidos.Contains(y)));

        if (filtro.CodEstadoSolicitud.HasValue)
            query = query.Where(x => x.CODESTADOCERTIFICACION == filtro.CodEstadoSolicitud);

        if (filtro.FechaIniSolicitud.HasValue)
        {
            if (!filtro.FechaFinSolicitud.HasValue)
                filtro.FechaFinSolicitud = new DateTime(DateTime.Now.Year   1, 12, 31);

            query = query.Where(x => x.FECHACREACION >= filtro.FechaIniSolicitud && x.FECHACREACION <= filtro.FechaFinSolicitud);
        }
        else if (filtro.FechaFinSolicitud.HasValue)
        {
            filtro.FechaIniSolicitud = new DateTime(DateTime.Now.Year - 1, 1, 1);
            query = query.Where(x => x.FECHAFIN >= filtro.FechaIniSolicitud && x.FECHAFIN <= filtro.FechaFinSolicitud);
        }

        return query.Select(c => new FiltroItem
        {
            CodEmpleado = c.CODEMPLEADO,
            MotivoCertificacion = ((EnumMotivoCertificacion)c.CODMOTIVOCERTIFICACION).ToString(),
            Estado = ((EnumPPEstadoCertificacion)c.CODESTADOCERTIFICACION).ToString(),
            FechaSolicitud = c.FECHACREACION,
            CodCertificacion = c.CODCERTIFICACION,
            UsuarioCertificadoLab = c.USUARIOASIGNADO.Split('|')[0],
            UsuarioCertificadoSal = c.USUARIOASIGNADO.Split('|')[1],
        })
            .OrderBy(x => x.FechaSolicitud)
            .ToList();
    }

CodePudding user response:

The issue is not LINQ itself but the specific LINQ provider. LINQ is basically "syntactic sugar" and it is up to the specific LINQ provider to convert your LINQ code into something useful for querying the specific lists that it supports. Entity Framework has its own LINQ provider called LINQ to Entities and that provider has to convert your LINQ code into SQL that can be executed against the underlying database. That means that certain code that is perfectly valid in C# cannot be executed at run time because there is no valid conversion to SQL. In your specific case, the issue appears to be here:

UsuarioCertificadoLab = c.USUARIOASIGNADO.Split('|')[0],
UsuarioCertificadoSal = c.USUARIOASIGNADO.Split('|')[1],

Based on the error message, it seems like the Split part can be done but indexing the resulting array is invalid. You'll have to find some other way to get those substrings. Maybe First and Last would work, or maybe you'll have to just pull back the full text and split it locally.

  • Related