title: Show data in one view in one to many relationship .net entity framework
body: <p>I have two tables in my DB: Student(many) and Department (one) and a one to many relationship. I want to show a list with DepartmentName for each StudentName, but I get this error: System.NullReferenceException: Object reference not set to an instance of an object.</p>
  
  <p>This is my controller code (the code works fine for one to one relations):</p>
  
  <pre><code>        public ActionResult Index()
      {
  
          List&lt;Students&gt; studlist = db.Students.ToList();
          StudentViewModel studlistVM = new StudentViewModel();
  
          List&lt;StudentViewModel&gt; studlistVMList = studlist.Select(x =&gt; new StudentViewModel
          {
              StudID = x.StudID,
              StudName = x.StudName,
              DeptName = x.deptobj.DeptName
  
  
          }).ToList();
  
  
          return View(studlistVMList);
      }
  </code></pre>