0
I have the following code:
var ret = Monitoramento.List
            .Include(p => p.CD)
            .ThenInclude(p => p.CargaEntrega)
            .ThenInclude(p => p.CargaEntrega.Motorista)
            .ThenInclude(p => p.CargaEntrega.Veiculo)
            .ThenInclude(p => p.CargaEntrega.Entregas)
            .ThenInclude(p => p.CargaEntrega.Entregas.Select(s => s.Cliente))
            .ThenInclude(p => p.CargaEntrega.Entregas.Select(s => s.NotasFiscais))
            .GetQuery()
            .Where(
                p => !p.IsDeleted
                && (p.Data >= dateInicio && p.Data <= dateFinal)
                && (!String.IsNullOrEmpty(filtro.motoristaId) ? 
                    p.CargaEntrega.Motorista.Id.ToString() == filtro.motoristaId : 1 == 1)
                && (!String.IsNullOrEmpty(filtro.unidadeNegociosId) ? 
                    p.CD.Id.ToString() == filtro.unidadeNegociosId : 1 == 1)
                && (!String.IsNullOrEmpty(filtro.status) ? 
                    statusBuscado.Contains(p.StatusMonitoramento) : 1 == 1)
                && (!String.IsNullOrEmpty(filtro.nroTransporte) ? 
                    "" + p.CargaEntrega.Entregas.FirstOrDefault().NroTransporte == filtro.nroTransporte : 1 == 1)
                && (!String.IsNullOrEmpty(filtro.uf) ? 
                    "" + p.CargaEntrega.Entregas.FirstOrDefault().Cliente.Estado == filtro.uf : 1 == 1)                    
             )
            .Select(MonitoramentoDto.ToDto)
            .ToArray();
        // Garantia de que os monitoramentos obedecem as duas regras:
        // Não exibe entregas que não possuem nota fiscal
        // Não exibe monitoramentos que não possuem entregas
        foreach (MonitoramentoDto itemMonitoramento in ret)
        {
            if(itemMonitoramento.Entregas.Length > 0)
            {
                List<EntregaDto> entregas = new List<EntregaDto>();
                foreach (EntregaDto entrega in itemMonitoramento.Entregas)
                {
                    if(entrega.NotaFiscal.Count() > 0)
                    {
                        entregas.Add(entrega);
                    }
                }
                if (entregas.Count > 0)
                {
                    itemMonitoramento.Entregas = entregas.ToArray();
                    monitoramentoLimpo.Add(itemMonitoramento);
                }
            }
        }
The intention is to remove the foreach below and add it directly to the variable query ret. The foreach removes deliveries that do not have tax bills, being that Notafiscal is a IList within the IList delivery.
Any idea how I can optimize this query?
In fact what I need is to check if the list of bills within the list of deliveries (of each delivery) have at least 1 record, those that have nothing or are null should not bring, would be something like SELECT * FROM Delivery Left Outer Join Notafiscal on Delivery.Id = Notafiscal.Delivery Where Notafiscal.Id is not null
– Guilherme Kuhnen
updated my comment, look there
– Ayrton Giffoni
That’s right, thank you!
– Guilherme Kuhnen