title: C# .net 3.5+ Dictionary<string, string> key/value pair displaying value as default choice – MVC select list
body: <p>I’m trying to figure out any way to retrieve certain elements of a particular class instance for use within a drop-down list that I’m attempting to perform some maintenance on. The goal is to select and assign all Contacts to a key/value pair string array (Contacts being a related entity of Company, specifically, a 1:n relationship - 1 company has multiple contacts) then selecting the first contact’s first and last name attributes out of the sequence where the Boolean class attribute, Primary, is set to True.  </p>
  
  <pre><code>public class Company 
  {
      public string Name { get; set; }
      public virtual ICollection&lt;Contact&gt; Contacts { get; set; }
  }
  
  public class Contact
  {
          public string FirstName { get; set; }
          public string LastName { get; set; }
          public bool IsPrimary { get; set; }
  }
  </code></pre>
  
  <p>The following code:  </p>
  
  <pre><code>private Company CreateViewModel(Company company)
  {
      var contacts = _contactService.CompanyContacts(company);
  
  }
  
  public Dictionary&lt;string, string&gt; CompanyContacts(Company company)
              {
                      var results = new Dictionary&lt;string, string&gt;();
  
                      foreach (var contact in company.Contacts)
                      {
                              var key = string.Format("{0} {1}|{2}", contact.FirstName, contact.LastName, contact.IsPrimary());
  
                              var val = string.Format("{0} {1}|{2}", contact.FirstName, contact.LastName, contact.IsPrimary());
  
                              if (results.ContainsKey(key)) continue;
  
                              results.Add(key, val);
                      }
  
                      return results;
              }
  
      public void InitContactList(Dictionary&lt;string, string&gt; contacts)
      {
  
          var firstOrDefault = contacts
              .Where(x =&gt; x.Key.Contains("true"))
              .Select(x =&gt; x.Value)
              .FirstOrDefault();
  
          var selectedContact = contacts.FirstOrDefault().Value;
  
              if (firstOrDefault != null)
              {
                  var selectedContact =  firstOrDefault;
  
                  ContactList = new SelectList(contacts, "Key", "Value")
                  .SelectByText(selectedContact.ToString()).ToList();
              }
  
              ContactList = new SelectList(contacts, "Key", "Value")
                  .SelectByText(selectedContact.ToString()).ToList();
      }
  </code></pre>
  
  <p>doesn't display the first 'is primary' contact by default in the select-list though. I would like the first isPrimary=1 contact to be the first selection in the picklist. I’m pretty sure the firstOrDefault variable in the InitContactList needs to have the key do something other than .contains(“true”) Any help would be appreciated.
  Thank you in advance.</p>