Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error with Linq query especific ArrayIndex

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading