title: How can I return single record of an IEnumerable object as part of my viewmodel?
body: <p>I have a viewmodel:</p>
  
  <pre><code>public class CourseViewModel
  {
      public int CourseID { get; set; }
      public string Title { get; set; }
      public string page_title { get; set; }
      public string titleabbrev { get; set; }
      public string Meta { get; set; }
      public string intro_1 { get; set; }
      public string intro_2 { get; set; }
      public string intro_3 { get; set; }
      public IEnumerable&lt;course_section&gt; Course_section { get; set; }
      public IEnumerable&lt;course_subsection&gt; Course_subsection { get; set; }
      public Enrollment Enrollments { get; set; }
  }
  </code></pre>
  
  <p>In my controller I have the following:</p>
  
  <pre><code>var allcourse = db.Course_page
              .Where(i =&gt; i.titleabbrev == courseabbrev)
          .Include(i =&gt; i.course_sections.Select(c =&gt; c.course_subsections))
          .Include(i =&gt; i.enrollments);
  
  
  
          var viewModel = new CourseViewModel();
  
          viewModel.CourseID = allcourse.Single().CourseID;
          viewModel.page_title = allcourse.Single().page_title;
          viewModel.titleabbrev = allcourse.Single().titleabbrev;
          viewModel.Title = allcourse.Single().Title;
          viewModel.intro_1 = allcourse.Single().intro_1;
          viewModel.intro_2 = allcourse.Single().intro_2;
          viewModel.intro_3 = allcourse.Single().intro_3;
  
          viewModel.Course_section = allcourse
              .Single().course_sections
              .OrderBy(i =&gt; i.Order);
  
  
          viewModel.Course_subsection = viewModel.Course_section
              .SelectMany(c =&gt; c.course_subsections)
              .Where(i =&gt; i.Titleabbrev == sectionabbrev)
              .OrderBy(i =&gt; i.Order);
  
  
          return View(viewModel);
  </code></pre>
  
  <p>I want to send to the view viewmodel.enrollments, however I'm having problems with doing that as I only want to send a single enrollment record to the view in the viewmodel and within the course_page model enrollment is IEnumerable.</p>
  
  <p>Presumably I should take only the single enrollment record when I am creating 'allcourse', however no matter what I try I don't seem to be able to do this and then return it to the view.  I've tried for example:</p>
  
  <pre><code>var allcourse = db.Course_page
              .Where(i =&gt; i.titleabbrev == courseabbrev)
              .Where(b =&gt; b.enrollments.Single().UserID == User.Identity.GetUserId())
          .Include(i =&gt; i.course_sections.Select(c =&gt; c.course_subsections))
          .Include(i =&gt; i.enrollments);
  </code></pre>
  
  <p>What is the best way to send the viewmodel to the view with only the enrollment record that matches the currently logged in userID?</p>
  
  <p>Edit - I've also tried things like:</p>
  
  <pre><code>var allcourse = db.Course_page
              .Where(i =&gt; i.titleabbrev == courseabbrev)
              Where(i =&gt; i.Enrollments.Any(b =&gt; b.UserID == User.Identity.GetUserId())
          .Include(i =&gt; i.course_sections.Select(c =&gt; c.course_subsections))
          .Include(i =&gt; i.enrollments);
  </code></pre>
  
  <p>Testing this in linqpad returned enrollment records where the UserID did not match.</p>
  
  <p>Thanks</p>