0

I am trying to reach a foreach but my program never gets inside because my ICollection Coletores is always empty even if I put a lot of stuff in there.

The code where I want to get inside and it is always empty (I want to get inside the second foreach):

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        if (NavigationContext.QueryString.TryGetValue("email", out email))
        {
            //MessageBox.Show(email);
            List<HyperlinkButton> listaLinks = new List<HyperlinkButton>();

            AppDataContext db = new AppDataContext();

            int i = 0;

            HyperlinkButton aux = new HyperlinkButton();

            foreach (Pessoa pessoa in db.Pessoas)
            {
                if (pessoa.Email == email)
                {
                    foreach (Coletor coletor in pessoa.Coletores)
                    {
                        aux.Content = "Coletor " + (i + 1).ToString();
                        aux.FontSize = 24;
                        aux.NavigateUri = new Uri("/OcorrenciasPage.xaml?coletorId=" + coletor.Id.ToString(), UriKind.RelativeOrAbsolute);
                        listaLinks.Add(aux);
                        i++;
                    }
                }
            }

            ListBox coletores = new ListBox();
            coletores.ItemsSource = listaLinks;
            stcList.Children.Add(coletores);
        }
        base.OnNavigatedTo(e);
    }

Now the code that I am adding data:

        if (txtLat.Text != "" && txtLong.Text != "" && rdNorte.IsChecked == true || rdSul.IsChecked == true && rdLeste.IsChecked == true || rdOeste.IsChecked == true)
        {
            foreach (var pessoa in db.Pessoas)
            {
                if (pessoa.Email == email)
                {
                    pessoa.Coletores.Add(coletor);
                }
            }

            db.Coletores.InsertOnSubmit(coletor);
            db.SubmitChanges();

            NavigationService.Navigate(new Uri("/ColetoresPage.xaml?email=" + email, UriKind.RelativeOrAbsolute));
        }

Now, my Pessoa class:

public class Pessoa : INotifyPropertyChanged
{
    private int _id;
    [Column(IsDbGenerated = true, IsPrimaryKey = true)]
    public int Id { 
        get { return _id;}
        set { _id = value;
        OnPropertyChanged("Id");
        }
    }

    private string _nome;
    [Column]
    public string Nome
    {
        get { return _nome; }
        set { _nome = value;
        OnPropertyChanged("Nome");
        }
    }

    private string _email;
    [Column]
    public string Email
    {
        get { return _email; }
        set { _email = value;
        OnPropertyChanged("Email");
        }
    }

    private string _senha;
    [Column]
    public string Senha { get { return _senha; }
        set { _senha = value;
        OnPropertyChanged("Senha");
        }
    }

    private string _profissao;
    [Column]
    public string Profissao { get { return _profissao; }
        set { _profissao = value;
        OnPropertyChanged("Profissao");
        }
    }

    private int _idade;
    [Column]
    public int Idade { get { return _idade; }
        set { _idade = value;
        OnPropertyChanged("Idade");
        }
    }

    private string _endereco;
    [Column]
    public string Endereco { get { return _endereco; }
        set { _endereco = value;
        OnPropertyChanged("Endereco");
        }
    }

    private string _cidade;
    [Column]
    public string Cidade { get { return _cidade; }
        set { _cidade = value;
        OnPropertyChanged("Cidade");
        }
    }

    private string _estado;
    [Column]
    public string Estado { get { return _estado; }
        set { _estado = value;
        OnPropertyChanged("Estado");
        }
    }

    private EntitySet<Coletor> _coletores = new EntitySet<Coletor>();

    [Association(Name = "FK_Coletores_PessoaColetores", Storage = "_coletores", ThisKey = "Id", OtherKey = "pessoaId")]
    public ICollection<Coletor> Coletores
    {
        get { return _coletores; }
        set { _coletores.Assign(value); }
    }

    private EntitySet<PessoaColetor> _pessoaColetores = new EntitySet<PessoaColetor>();

    [Association(Name = "FK_PessoaColetores_Pessoas", Storage = "_pessoaColetores", OtherKey = "pessoaId", ThisKey = "Id")]
    private ICollection<PessoaColetor> PessoaColetores
    {
        get { return _pessoaColetores; }
        set { _pessoaColetores.Assign(value); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
}
Gustavo Mendonça
  • 1,925
  • 3
  • 15
  • 26

1 Answers1

0

The Coletores property on the Pessoa class looks alright. There is also a PessoaColetores property. Are you sure there has been no confusion between the two? Especially with intellisense one might dot or tab the wrong one.

Have you put a break point on the line that actually adds coletors (second code snippet)? Maybe stuff is added to the wrong collection.

Cheers, B.

DevBezz
  • 93
  • 1
  • 5
  • I don't think that has been a confusion because my PessoaColetor class only contains a 1:M relationship to the Pessoa and to the Coletor classes. Yes, I have put a break point on that line and it is adding right, both the ICollection counting increases, but when it reaches my first foreach, it becomes empty my ICollection and the counting becomes 0. – Gustavo Mendonça Mar 11 '15 at 12:47