title: Using the Repository pattern - where is acceptable to use LINQ filtering?
body: <p>I've recently started implementing the Repository pattern into my service, and I've come across a small niggle that I'd like suggestions about.</p>
  
  <p>Take the following code, in my service:</p>
  
  <pre><code>[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped)]
  public Policy GetPolicyById(int policyId, int historyId)
  {
      // Don't do the if not deleted check here, as we can do it on our Controller.
      // This means we can use this function if we have to access the deleted records.
      return _policy.GetByID(policyId, historyId);
  }
  </code></pre>
  
  <p>This function is obviously referenced directly from the service interface, and the .GetByID method is part of my generic repository.</p>
  
  <p>The main question is: what if I want to do a <code>.Where(a =&gt; !a.Deleted)</code> on this result set. You can see by my code comment what my general idea of thinking is, but is this the "correct" way of doing it, should it be done at the service level, the Controller (UI) level, or should a function be created within my non-generic repository to allow for this functionality?</p>
  
  <p>** UPDATE **</p>
  
  <p>This is another portion of my code, in my policies repository:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// 
  /// &lt;/summary&gt;
  /// &lt;param name="policyId"&gt;&lt;/param&gt;
  /// &lt;param name="historyid"&gt;&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  public virtual IEnumerable&lt;Policy&gt; GetPolicies(int take, int skip)
  {
      var policies = GetPoliciesHistoryGrouped().Take(take).Skip(skip);
      return policies;
  }
  
  /// &lt;summary&gt;
  /// Returns all non-deleted (bitDelete) policies (in an expression, not comitted via the database), 
  /// grouped by the latest history ID.
  /// &lt;/summary&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  private IEnumerable&lt;Policy&gt; GetPoliciesHistoryGrouped()
  {
      // Group and order by history ID.
      var historyIDs = _context.Policies.Where(a =&gt; !a.bitDelete)
                       .GroupBy(a =&gt; a.intPolicyId).Select(group =&gt; new
                       {
                           PolicyID = group.Key,
                           HistoryID = group.Max(a =&gt; a.intHistoryID)
                       });
      var query = (from h in historyIDs
                   join p in _context.Policies on new { h.HistoryID, h.PolicyID } equals new { HistoryID = p.intHistoryID, PolicyID = p.intPolicyId }
                   select p).OrderBy(a =&gt; a.intPolicyId);
      return query;
  }
  </code></pre>
  
  <p>Should this level of depth should not be in the repository layer, instead be in the service layer, in which the <code>GetPoliciesHistoryGrouped</code> function would still have to be created and shared? Remember, .Take and .Skip cannot be called until after the Grouping has been done.</p>