title: asp.net mvc 5 get related data from different tables
body: <p>I'm trying to get specific data from different tables based on some requirements, The program is supposed to take some columns of choosing from tables: Contatti, Contacts and Companies which are at different locations and bring them together.</p>
  
  <p>Contact is connected to Companies through CompanyID</p>
  
  <p>What i want is to display the company name basing on the CompanyID field in Contact table. The problem is that i iterate through a list to get the data in the view, and because of that i can't seem to get a company name based on the company ID attribute which is the Contact Foreign Key to the Companies table, what i get is all the companies names, because they're in the same list.</p>
  
  <p>I'm sure there is an easy way to do this but this is a new world for me, thank you for any help.</p>
  
  <p>Contact Model:</p>
  
  <pre><code>public class Contact
  {
  [Key]
  public int ContactId { get; set; }
  public string Name { get; set; }
  public string Address { get; set; }
  public string City { get; set; }
  public string State { get; set; }
  public string Zip { get; set; }
  [DataType(DataType.EmailAddress)]
  public string Email { get; set; }
  [ForeignKey("Companies")]
  public int CompanyId { get; set; }
  public virtual ICollection&lt;Companies&gt; Companies { get; set; }
  [Required]
  //public virtual Contatti Contatti { get; set; }
  public virtual ICollection&lt;Contatti&gt; Contatti { get; set; }
  }
  </code></pre>
  
  <p>Companies model:</p>
  
  <pre><code>public class Companies
  {
  [Key]
  public int CompanyId { get; set; }
  public string CompanyName { get; set; }
  public string CompanyAddress { get; set; }
  public string CompanyCity { get; set; }
  public string CompanyState { get; set; }
  public string CompanyZip { get; set; }
  public string CompanyArea { get; set; }
  }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>public ActionResult Index(String Page)
      {
  
          ContactsUni2 CU = new ContactsUni2();
          CU.Contattis = db2.Contatti.ToList();
          CU.Contacts = db.Contacts.ToList();
          CU.Companies = db.Companies.ToList();
          List&lt;ContactsUni2&gt; contactlist = new List&lt;ContactsUni2&gt;();
          contactlist.Add(CU);
          return View(contactlist);
      }
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>    @foreach (var item in Model)
      {
          for (int i = 0; i &lt; item.Contacts.Count; i++)
          {
              &lt;tr&gt;
                  &lt;td&gt;
                      @item.Contacts[i].ContactId
                  &lt;/td&gt;
                  &lt;td&gt;
                      @item.Contacts[i].Name
                  &lt;/td&gt;
                  &lt;td&gt;
                      @item.Contacts[i].Address
                  &lt;/td&gt;
                  &lt;td&gt;
                      @item.Contacts[i].CompanyId
                  &lt;/td&gt;
                      @if (@item.Contacts[i].CompanyId == item.Companies[i].CompanyId)
                          {
                      &lt;td&gt;
                          @item.Companies[i].CompanyName
                      &lt;/td&gt;
                      &lt;td&gt;
                          @item.Companies[i].CompanyCity
                      &lt;/td&gt;
                      &lt;td&gt;
                          @item.Companies[i].CompanyArea
                      &lt;/td&gt;
                          }
                      }
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;
                          @item.Contattis[i].ContattoID
                      &lt;/td&gt;
                      &lt;td&gt;
                          @item.Contattis[i].Nome
                      &lt;/td&gt;
                      &lt;td&gt;
                          @item.Contattis[i].Citta
                      &lt;/td&gt;
                      &lt;td&gt;
                          @item.Contattis[i].CodicePostale
                      &lt;/td&gt;
                      &lt;td&gt;
                          @item.Contattis[i].Email
                      &lt;/td&gt;
              &lt;/tr&gt;
  
  
          }
      }
  
  &lt;/table&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>