none
Como Optimizar consulta ? RRS feed

  • Pregunta

  • Hola ...

    Tengo la siguiente consulta, y quiero saber si puedo optimizar el siguiente codigo , eliminando el foreach :

      public IList<GuestModel> Recipients(int id_Campaing)
            {
                List<GuestModel> destinatarios = new List<GuestModel>();
                using (SocialWifiContext context = new SocialWifiContext())
                {
                    var r = (from guests in context.Guests
                             join campaign_messages in context.Campaign_Messages
                             on guests.Id equals campaign_messages.Id_Guest
                             where campaign_messages.Id_Campaign == id_Campaing
                             select new
                             {
                                 guests.Id,
                                 guests.Name,
                                 guests.Last_Name,
                                 guests.Phone,
                                 guests.Email,
                                 guests.Id_Gender,
                                 guests.Location,
                                 guests.IdFcm,
                                 campaign_messages.Id_Guest,
                                 campaign_messages.Id_Campaign
                             }).ToList();
    
                    foreach (var i in r)
                    {
                        GuestModel single = new GuestModel()
                        {
                            Id = i.Id,
                            Name = i.Name,
                            Last_Name = i.Last_Name,
                            Phone = i.Phone,
                            Email = i.Email,
                            Id_Gender = i.Id_Gender,
                            Location = i.Location,
                            IdFcm = i.IdFcm,
                            Id_Campaing = i.Id_Campaign,
                            Id_Guests = i.Id_Guest
    
                        };
                        destinatarios.Add(single);
                    }
                }
                return destinatarios;
            }


    EFRAIN MEJIAS C VALENCIA - VENEZUELA

    viernes, 20 de diciembre de 2019 12:44

Respuestas

  • hola

    Podrias usar

      var destinatarios = (from guests in context.Guests
                             join campaign_messages in context.Campaign_Messages
                             on guests.Id equals campaign_messages.Id_Guest
                             where campaign_messages.Id_Campaign == id_Campaing
                             select new GuestModel()
    				{
    					Id = guests.Id,
    					Name = guests.Name,
    					Last_Name = guests.Last_Name,
    					Phone = guests.Phone,
    					Email = guests.Email,
    					Id_Gender = guestsi.Id_Gender,
    					Location = guests.Location,
    					IdFcm = guests.IdFcm,
    					Id_Campaing = campaign_messages.Id_Campaign,
    					Id_Guests = campaign_messages.Id_Guest
    
    				}).ToList();
    defines la class directo en el select

    saludos


    Leandro Tuttini

    Blog
    MVP Profile
    Buenos Aires
    Argentina

    viernes, 20 de diciembre de 2019 15:24