Search code examples
c#entity-frameworklambdasubquery

The nested query is not supported. Operation1='Case' Operation2='Collect


The following sentence is throwing this exception: The nested query is not supported. Operation1='Case' Operation2='Collect

_contexto.PESO_PIE.Where(w => w.LOTE_SACRIFICIO.LOTE.ID_ESTATUS_LOTE != _estatus_cerrado)
                    .GroupJoin(_contexto.PESO_CALIENTE,
                    PP => PP.ID_SACRIFICIO,
                    pc => pc.ID_SACRIFICIO,
                    (x, y) => new { PP = x, pc = y.DefaultIfEmpty() })
                    .SelectMany(a => a.pc.Select(b => new { PP = a.PP, pc = b }))
                    .Select(s => new cPeso_Caliente() {
                        Sec_Sacrificio = s.PP.SEC_SACRIFICIO,
                        Cliente = s.pc != null ? s.pc.DESTINO_CLIENTE.DESTINO_CLIENTE1 : null,
                        Consec_Dia = s.pc != null ? s.pc.CONSEC_DIA : (int?)null,
                        Es_Maquila = s.PP.LOTE_SACRIFICIO.ORDEN_COMPRA.PROVEEDOR.ES_MAQUILA,
                        Fecha_Registro = s.pc != null ? s.pc.FECHA_REGISTRO : (DateTime?)null,
                        Folio_Lote = s.PP.LOTE_SACRIFICIO.LOTE.ID_LOTE, // cambiar por folio lote despues de merge
                        Folio_Orden_Compra = s.PP.LOTE_SACRIFICIO.ORDEN_COMPRA.ID_ORDEN_COMPRA, //cambiar por folio orden sacrificio despues de merge
                        IC = s.pc != null ? s.pc.IC : (byte?)null,
                        Id_Destino_Cliente = s.pc != null ? s.pc.ID_DESTINO_CLIENTE : (long?)null,
                        Id_Estado_Producto = s.pc != null ? s.pc.ETIQUETA.ID_ESTADO_PRODUCTO : null,
                        Id_Etiqueta = s.pc != null ? s.pc.ID_ETIQUETA : null,
                        Id_Proveedor = s.PP.LOTE_SACRIFICIO.ORDEN_COMPRA.ID_PROVEEDOR,
                        Id_Sacrificio = s.PP.ID_SACRIFICIO,
                        Id_Usuario = s.pc != null ? s.pc.ID_USUARIO : null,
                        IFE = s.pc != null ? s.pc.IFE : (byte?)null,
                        IGC = s.pc != null ? s.pc.IGC : (byte?)null,
                        IGP = s.pc != null ? s.pc.IGP : (byte?)null,
                        MAD = s.pc != null ? s.pc.MAD : (byte?)null,
                        M_30 = s.pc != null ? s.pc.M_30 : (bool?)null,
                        Proveedor = s.PP.LOTE_SACRIFICIO.ORDEN_COMPRA.PROVEEDOR.NOMBRE_COMERCIAL,
                        R1 = s.pc != null ? s.pc.R1 : false,
                        Version = s.pc != null ? s.pc.VERSION : null,
                        GPE = s.pc != null ? s.pc.GPE : (byte?)null,
                        Tipo_Ganado = s.PP.LOTE_SACRIFICIO.TIPO_GANADO.TIPO_GANADO1,
                        Peso_Caliente = s.pc != null ? s.pc.ETIQUETA.PESO : (decimal?)null,
                        Peso_Pie= s.PP.PESO_PIE1,
                        Id_Producto=s.PP.LOTE_SACRIFICIO.ORDEN_COMPRA.DETALLE_OC.FirstOrDefault().ID_PRODUCTO,
                        Id_Lote_Sacrificio=s.PP.ID_LOTE,
                        Id_Tipo_Lote=s.PP.ID_TIPO_LOTE,
                        Log_Etiqueta_Anomalia= s.pc !=null && s.pc.ETIQUETA.ID_ESTADO_PRODUCTO!="N"? 
                        s.pc.ETIQUETA.LOG_ETIQUETA_ANOMALIA.Select(sa=>new cLog_Etiqueta_Anomalia {
                            Id_Anomalia= sa.ID_ANOMALIA,
                            Id_Etiqueta= sa.ID_ETIQUETA,
                            Id_Log_Etiqueta= sa.ID_LOG_ETIQUETA,
                            Id_Resultado_Anomalia= sa.ID_RESULTADO_ANOMALIA,
                            Version= sa.VERSION
                        }): null 
                        }).OrderBy(o=>o.Sec_Sacrificio).AsEnumerable()

I know the problem is in the following part of the query

Log_Etiqueta_Anomalia= s.pc !=null && s.pc.ETIQUETA.ID_ESTADO_PRODUCTO!="N"? 
                        s.pc.ETIQUETA.LOG_ETIQUETA_ANOMALIA.Select(sa=>new cLog_Etiqueta_Anomalia {
                            Id_Anomalia= sa.ID_ANOMALIA,
                            Id_Etiqueta= sa.ID_ETIQUETA,
                            Id_Log_Etiqueta= sa.ID_LOG_ETIQUETA,
                            Id_Resultado_Anomalia= sa.ID_RESULTADO_ANOMALIA,
                            Version= sa.VERSION
                        }): null 

I have read the related questions to this exceptions but i cant find something that could solve this problem in specific in lambdas.

Is there a way to accomplish this or EF cant handle this query?

Take into account that PESO_CALIENTE and SACRIFICIO have a 1:1 relationship


Solution

  • Sine nested queries are not supported, I think you should define a separate method to get the value of Etiqueta_Anomalia:

    Etiqueta_Anomalia = GetEtiqueta_Anomalia(s, sa);
    

    By moving the replaced logic to the new method. This way you will also make your query more readable.