title: Chaining linq queries
body: <p>Attempting to chain linq queries due to creating a simple filter for the datatables in my view. </p>
  
  <pre><code>public ActionResult Index(int? courseId, string grade, int? statusId)
      {
          var query = from s in db.Students
                      join e in db.Enrollments on s.ID equals e.ID
                      join c in db.Courses on e.CourseID equals c.ID
                      orderby e.Grade.HasValue descending, e.Grade, s.StatusID
                      select new { s, e, c };           
  
          if (courseId != null)
          {
              query = query.Where(q =&gt; q.e.CourseID == courseId);
          }
  
          if (!string.IsNullOrEmpty(grade))
          {
              query = query.Where(q =&gt; q.e.Grade.Equals(grade));
          }
  
          if (statusId != null)
          {
              query = query.Where(q =&gt; q.s.StatusID == statusId);
          }
  
          query.Select( q =&gt; new ViewModels.ReportView
                  {
                      ID = q.s.ID,
                      Name = q.s.FirstName + " " + q.s.LastName,
                      Course = q.c.Title,
                      Grade = q.e.Grade,
                      GraduationDate = q.s.GraduationDate,
                      Status = q.s.Status.Title
                  });
  </code></pre>
  
  <p>Linq forces you to end queries with a select or group which is forcing me to put an unnecessary select at the start. For some reason this is causing the it to ignore the last select and just use the initial.</p>
  
  <p>I've tried grouping instead by e.ID but that doesn't work as expected...</p>
  
  <p>I'm sure this is simple but I'm lost, any ideas?</p>