Foreach does not stop

0

Hello my doubt is that I have a foreach that does not stop:

foreach (var i in establecimientos)
                        {

                            foreach (var promocionPlantilla in plantillaHabilitados)
                            {
                                List<PromocionEstablecimientos> promocionEstablecimientos =
                                        (from pe in bd.PromocionEstablecimientos
                                         select pe)
                                        .Where(x => x.EstablecimientoId == i.EstablecimientoId
                                        && x.PromocionId == promocionPlantilla.PromocionId)
                                        .ToList();

                                if (promocionEstablecimientos.Count() <= 0) //No Existe
                                {
                                    //Creamos la promocion establecimiento
                                    PromocionEstablecimientos nuevo = new PromocionEstablecimientos();
                                    nuevo.PromocionEstablecimientoId = Guid.NewGuid();
                                    nuevo.PromocionId = promocionPlantilla.PromocionId;
                                    nuevo.EstablecimientoId = i.EstablecimientoId;
                                    nuevo.CodigoProducto = promocionPlantilla.CodigoProducto;
                                    nuevo.CodigoProductoBonificado = promocionPlantilla.CodigoProductoBonificado;
                                    nuevo.Habilitado = promocionPlantilla.Habiitado;


                                    bd.PromocionEstablecimientos.Add(nuevo);
                                }else
                                {
                                    PromocionEstablecimientos xy =
                                        (from pe in bd.PromocionEstablecimientos
                                         select pe)
                                        .Where(x => x.EstablecimientoId == i.EstablecimientoId
                                        && x.PromocionId == promocionPlantilla.PromocionId)
                                        .FirstOrDefault();

                                    if (xy != null)
                                    {
                                        xy.Habilitado = true;
                                        xy.CodigoProducto = promocionPlantilla.CodigoProducto;
                                        xy.CodigoProductoBonificado = promocionPlantilla.CodigoProductoBonificado;
                                    }

                                }
                            }

                        }

                    }
                    bd.SaveChanges();
                }

                blResultado = true;
            }

I know it's fine because in the bd it adds but the program does not stop.

    
asked by Samuel CM 21.10.2017 в 02:17
source

1 answer

0

You can put the number of items within "establishments" and multiply it by the number of items in "Enabled template" ?, since according to Number of establishments will visit the list of DisabledTemplate so many times.

At the end it always ends up going through the list, provided that another method is not filling one of the two previous lists.

Greetings.

    
answered by 23.10.2017 в 16:24