Check for list records in the Entity framework

Asked

Viewed 496 times

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?

1 answer

2


From what I can see, just put that condition inside your where:

&& p.SeuModelAteChegarNasEntregas.Entregas.Any()

After that, he will only bring those who have deliveries.

You can add after the select:

.Select(MonitoramentoDto.ToDto).Where(m => m.Any(a => a.Entregas.Any()))

Edit1:

Having a list of deliveries:

Entregas.Where(m => m.NotasFiscais.Any())

Then you can use it in the first example I put up there.

  • 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

  • updated my comment, look there

  • That’s right, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.