title: ASP.NET MVC5 - (Entity Framework) LINQ Query Join & Where returning only 1 result but not other result if changed
body: <p>Okay. I will describe to the best of my abilities what I am trying to accomplish. I could not find any Stack Overflow questions that poses my question.</p>
  
  <p><strong>ASP.NET MVC5 - (Entity Framework) LINQ Query from DBContext Models into ViewModel</strong></p>
  
  <p>I am using ASP.NET MVC5 with Entity Framework and I have scaffolded the DBContext into the class <strong>models</strong> <code>Schools</code> and <code>Contacts</code>. I am trying to use LINQ Query to join the two models together and assign it into the <strong>ViewModel</strong> called <code>SchoolVM</code>.</p>
  
  <p>I joined <code>Contacts</code> with <code>Schools</code> through the <code>school.ID equals contact.School_ID</code></p>
  
  <p>So for the record <code>id = 1</code> in <code>Schools</code>, it does return the result that I wanted.</p>
  
  <p>But when I re-query for <code>id = 2</code> or <code>id = 3</code> and so forth, it returns nothing, with a count of zero.</p>
  
  <p>Just to let everyone know, the records for id 2,3,4 and so on does exist.</p>
  
  <p>Can anybody help me with this? Let me know if you need more code information.</p>
  
  <p>Here is the Controller Action Method down below.</p>
  
  <pre><code>public ActionResult Details(int? id)
  {
      if (id == null)
      {
          return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
      }
  
      //**LINQ Query joining two Models and putting into a ViewModel.
      //**LINQ Query that seems to return the record ID == 1, but not
      //**existing record ID == 2 or 3 or so on and so forth.
      var schoolDetails = (from school in db.Schools
                           join contact in db.Contacts on school.ID equals contact.School_ID
                           where school.ID == id
                           select new { school, contact }).ToList();
  
      //**This is the ViewModel that I am trying to assign the joined LINQ query into.
      SchoolVM schoolVM = new SchoolVM();
  
      foreach (var value in schoolDetails)
      {
  
          //**Schools Model Information
          schoolVM.ID = value.school.ID;
          schoolVM.SchoolName = value.school.SchoolName;
          schoolVM.Address = value.school.Address;
          schoolVM.City = value.school.City;
          schoolVM.State = value.school.State;
          schoolVM.Zip = value.school.Zip;
          schoolVM.MainPhone = String.IsNullOrEmpty(value.school.MainPhone) ? "" : String.Format("{0:(###) ###-####}", double.Parse(value.school.MainPhone));
          schoolVM.Website = value.school.Website;
          schoolVM.NumberOfStudents = value.school.NumberOfStudents;
          schoolVM.SchoolOrDistrict = value.school.SchoolOrDistrict;
          schoolVM.CountyID = value.school.CountyID;
  
          //**Contacts Model Information
          schoolVM.ContactFirstName = value.contact.ContactFirstName;
          schoolVM.ContactLastName = value.contact.ContactLastName;
          schoolVM.ContactTitle = value.contact.ContactTitle;
          schoolVM.ContactPhone = value.contact.ContactPhone;
          schoolVM.ContactEmail = value.contact.ContactEmail;
          schoolVM.PrimaryOrSecondary = value.contact.PrimaryOrSecondary;
          schoolVM.Coordinator = value.contact.Coordinator;
      }
  
      //**This is where LINQ is returning COUNT = 0 for ID = 2, 3, 4 and so forth.
      if (schoolDetails == null || schoolDetails.Count == 0)
      {
          return HttpNotFound();
      }
  
      return View(schoolVM);
  }
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Thanks to Daniel Lorenz's input. Very simple solution. I just changed the LINQ Query from INNER JOIN or JOIN to LEFT JOIN.</p>
  
  <p><strong>ORIGINAL:</strong></p>
  
  <pre><code>var schoolDetails = (from school in db.Schools
                       join contact in db.Contacts on school.ID equals contact.School_ID
                       where school.ID == id
                       select new { school, contact }).ToList();
  </code></pre>
  
  <p><strong>SOLUTION:</strong></p>
  
  <pre><code>//**Changed alias name around a bit to accommodate the rest of the code.
  
  var schoolDetails = (from school in db.Schools
                       join contacts in db.Contacts on school.ID equals contacts.School_ID into schoolvm
                       from contact in schoolvm.DefaultIfEmpty()
                       where school.ID == id
                       select new { school, contact }).ToList();
  </code></pre>