title: C# How to use a 'foreach' statement using a data source?
body: <p>Hi guys I've made a owner drawn list box which adds avatar and the users friends name and email.I'm having a little trouble. Basically I have a query which gets the users friends names and email and then the query is added to a list box data source by using 'query.ToList()'. What I'm trying to do is for every friend in the data source, I want it so that it creates a new list box item.</p>
  
  <p>Code: </p>
  
  <pre><code>        FriendsLb.DisplayMember = "Display";
          FriendsLb.ValueMember = "FirstName";
          FriendsLb.ValueMember = "LastName";
          FriendsLb.ValueMember = "Email";
  
          var query = from o in Globals.DB.Friends
                      where o.UserEmail == Properties.Settings.Default.Email
                      select new
                      {
                          FirstName = o.FirstName,
                          LastName = o.LastName,
                          Email = o.Email,
  
                          Display = string.Format(" {0} {1} - ({2})", o.FirstName, o.LastName, o.Email),
                      };
  
          FriendsLb.DataSource = query.ToList();
  
          foreach (object contact in FriendsLb.DataSource))
          {
              FriendsLb.Items.Add(new Contacts(Properties.Resources.avatar, contact.ToString()));
          }
  </code></pre>
  
  <p>Error:
  <a href="https://i.stack.imgur.com/cVagp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cVagp.png" alt="Error"></a>
  Any ideas? Cheers</p>
  
  <p>This is what it should look like when the code is correct:</p>
  
  <p><a href="https://i.stack.imgur.com/Ik1pD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ik1pD.png" alt="Gui"></a></p>