title: LINQ Lambda: Getting Last item (where == ) of sub collection
body: <p>I have the following (which I've used in CodeFirst):</p>
  
  <pre><code>public class MyForm
      {
          public int MyFormId { get; set; }
          //Many Form Properties
          public virtual ICollection&lt;AuditItem&gt; AuditItems { get; set; }
      }
  
  public class AuditItem
      {
          public int AuditItemId { get; set; }
          public int MyFormId { get; set; }
          public IAppUser User { get; set; }
          public string ActionByUserStr { get; set; }
          public string ActionByUserDisplayName { get; set; }
          public DateTime DateOfAction { get; set; }
          public string TypeOfAction { get; set; }
          public int Step { get; set; }
  
          public virtual MyForm MyForm { get; set; }
      }
  </code></pre>
  
  <p>The AuditItem tracks many actions carried out on MyForm</p>
  
  <p>I'm presenting an index page showing a table where the AuditItem's last Step = 1.  </p>
  
  <p>I do an orderby DateOfAction (because the Step can get setback to 0, so I wouldn't want it to equal 1 unless it is also the last AuditItem Record for that MyForm), then Select the last record's step, then query on that.  I want to query as early as I can so that I'm not pulling back unrequired MyForms records.</p>
  
  <p>This is the query I have:</p>
  
  <pre><code>var myFormsAtStep1 =
                  context.MyForms.Include("AuditItems").Where(
                      mf =&gt; mf.AuditItems.OrderBy(ai =&gt; ai.DateOfAction).Last().Step == 1);
  </code></pre>
  
  <p>It gives this error:</p>
  
  <pre><code>LINQ to Entities does not recognize the method 'MyForms.Models.AuditItem Last[AuditItem](System.Collections.Generic.IEnumerable`1[MyForms.Models.AuditItem])' method, and this method cannot be translated into a store expression.
  </code></pre>