title: Linq with boolean function to relational db in Entity Framework
body: <p>Probably a few things wrong with my code here but I'm mostly having a problem with the syntax. Entry is a model for use in Entries and contains a TimeStamp for each entry. Member is a model for people who are assigned entries and contains an fk for Entry. I want to sort my list of members based off of how many entries the member has within a given period (arbitrarily chose 30 days). 
  A. I'm not sure that the function I created works correctly, but this is aside from the main point because I haven't really dug into it yet.
  B. I cannot figure out the syntax of the Linq statement or if it's even possible.</p>
  
  <p>Function:</p>
  
  <pre><code>        private bool TimeCompare(DateTime TimeStamp) 
      {
          DateTime bound = DateTime.Today.AddDays(-30);
  
          if (bound &lt;= TimeStamp)
          {
              return true;
          }
  
          return false;
      }
  </code></pre>
  
  <p>Member list:</p>
  
  <pre><code>    public PartialViewResult List()
      {
          var query = repository.Members.OrderByDescending(p =&gt; p.Entry.Count).Where(TimeCompare(p =&gt; p.Entry.Select(e =&gt; e.TimeStamp));
  
          //return PartialView(repository.Members);
          return PartialView(query);
      }
  </code></pre>
  
  <p>the var query is my problem here and I can't seem to find a way to incorporate a boolean function into a .where statement in a linq. </p>
  
  <p><strong><em>EDIT</em></strong>
  To summarize I am simply trying to query all entries timestamped within the past 30 days.</p>
  
  <p>I also have to emphasize the relational/fk part as that appears to be forcing the Timestamp to be IEnumerable of System.Datetime instead of simple System.Datetime.</p>
  
  <p>This errors with "Cannot implicitly convert timestamp to bool" on the E.TimeStamp:</p>
  
  <pre><code>var query = repository.Members.Where(p =&gt; p.Entry.First(e =&gt; e.TimeStamp) &lt;= past30).OrderByDescending(p =&gt; p.Entry.Count);
  </code></pre>
  
  <p>This errors with Operator '&lt;=' cannot be applied to operands of type 'System.Collections.Generic.IEnumerable' and 'System.DateTime'</p>
  
  <pre><code>var query = repository.Members.Where(p =&gt; p.Entry.Select(e =&gt; e.TimeStamp) &lt;= past30).OrderByDescending(p =&gt; p.Entry.Count);
  </code></pre>
  
  <p><strong><em>EDIT2</em></strong></p>
  
  <p>Syntactically correct but not semantically:</p>
  
  <pre><code>var query = repository.Members.Where(p =&gt; p.Entry.Select(e =&gt; e.TimeStamp).FirstOrDefault() &lt;= timeComparison).OrderByDescending(p =&gt; p.Entry.Count);
  </code></pre>
  
  <p>The desired result is to pull all members and then sort by the number of entries they have, this pulls members with entries and then orders by the number of entries they have. Essentially the .where should somehow be nested inside of the .count.</p>
  
  <p><strong><em>EDIT3</em></strong></p>
  
  <p>Syntactically correct but results in a runtime error (Exception Details: System.ArgumentException: DbSortClause expressions must have a type that is order comparable.
  Parameter name: key):</p>
  
  <pre><code>var query = repository.Members.OrderByDescending(p =&gt; p.Entry.Where(e =&gt; e.TimeStamp &lt;= timeComparison));
  </code></pre>
  
  <p><strong><em>EDIT4</em></strong></p>
  
  <p>Closer (as this line compiles) but it doesn't seem to be having any effect on the object. Regardless of how many entries I add for a user it doesn't change the sort order as desired (or at all).</p>
  
  <pre><code>        var timeComparison = DateTime.Today.AddDays(-30).Day;
          var query = repository.Members.OrderByDescending(p =&gt; p.Entry.Select(e =&gt; e.TimeStamp.Day &lt;= timeComparison).FirstOrDefault());
  </code></pre>