title: Strongly typed Linq filtering method
body: <p>I am a bit tired of writing lines of service layer codes like these:</p>
  
  <p>Below codes just an example for readers. So they may have errors or typos, sorry about it :)</p>
  
  <pre><code>//ViewModel
  public class EntityIndexFilterPartial
  {
      public int? Id { get; set; }
      public DateTime? StartDate { get; set; }
      public DateTime? EndDate { get; set; }
      public IEnumerable&lt;SelectListItem&gt; StatusList { get; set; }
      public int? StatusID { get; set; }
  }
  
  
  //Service Layer method
  //Method parameters are reperesents view model properties
  public IQueryable&lt;Entity&gt; FilterBy(int? id, DateTime? startDate, DateTime? endDate, int? statusId)
  {
      var filter = _db.Entities.AsQueryable();
      if (id.HasValue)
          filter = filter.Where(x =&gt; x.Id == id.Value);
      if (startDate.HasValue)
          filter = filter.Where(x =&gt; x.StartDate &gt;= startDate.Value);
      if (endDate.HasValue)
          filter = filter.Where(x =&gt; x.EndDate &lt;= endDate.Value);
      if (statusId.HasValue)
          filter = filter.Where(x =&gt; x.EntityStatus.StatusID == statusId);
      return filter;
  }
  </code></pre>
  
  <p>I search pretty enough for some smart designed codes. I know about <strong>Dynamic LINQ library</strong> and I use it too. But I'm looking for <strong>strongly typed filtering</strong>. 
  I don't want to write magic strings or some sort of them.</p>
  
  <p>So basically I found some solutions but I want to hear from this community for some well written smart codes. Of course there may be dozens of solutions but again how would you write your strongly typed filtering service layer codes. Any ideas...</p>
  
  <p>Here are few of my solutions:</p>
  
  <p><strong>Solution 1:</strong>
  Same FilterBy method but parameters are different, now taking expression list. So this means I'm creating predicateList in controller and send it to here.</p>
  
  <pre><code>public IQueryable&lt;Entity&gt; FilterBy(List&lt;Expression&lt;Func&lt;Entity,bool&gt;&gt;&gt; predicateList)
  {
      var filter = _db.Entities.AsQueryable();
      foreach (var item in predicateList)
      {
          filter = filter.FilterBy(item);
      }
      return filter;
  }
  </code></pre>
  
  <p><strong>Solution 2:</strong>
  FilterBy method taking the EntityIndexFilterPartial as parameter in Application Service layer(not domain service). I'm sure this design has some problems, but I want to hear your opinions.</p>
  
  <pre><code>public IQueryable&lt;Entity&gt; FilterBy(EntityIndexFilterPartial filterPartial)
  {
      //I'm calling the domain service layer FilterBy method such as like in solution 1.
  }
  </code></pre>
  
  <p><strong>Solution 3:</strong>
  I think this one much better than others but I'm still thinking for something more simple and better codes.</p>
  
  <pre><code>//helper class
  public static class FilterByHelper
  {
      public static IQueryable&lt;T&gt; If&lt;T&gt;(this IQueryable&lt;T&gt; filter, bool condition, Expression&lt;Func&lt;T, bool&gt;&gt; predicate)
      {
          if (condition)
              return filter.FilterBy(predicate);
          return filter;
      }
  
      public static IQueryable&lt;T&gt; FilterBy&lt;T&gt;(this IQueryable&lt;T&gt; filter, Expression&lt;Func&lt;T, bool&gt;&gt; predicate)
      {
          return filter.Where(predicate);
      }
  }
  
  
  public IQueryable&lt;Entity&gt; FilterBy(int? id, DateTime? startDate, DateTime? endDate, int? statusId)
  {
      return _db.Entities
          .If(id.HasValue, x =&gt; x.Id == id.Value)
          .If(startDate.HasValue, x =&gt; x.StartDate &gt;= startDate.Value)
          .If(endDate.HasValue, x =&gt; x.EndDate &lt;= endDate.Value)
          .If(statusId.HasValue, x =&gt; x.EntityStatus.StatusID == statusId);
  }
  </code></pre>
  
  <p>I know this became a bit long question but I hope I clearly ask what I want to ask.</p>
  
  <p><strong>As a quick and simple question do you know any smartly designed code for saving us from writing same lines of these filtering codes?</strong></p>
  
  <p>Btw, I'm not looking for desing pattern solutions or huge answers, you can give me some example or say how to find the better path is enough. </p>
  
  <p>Of course If you write a full explained response I will be appricated.</p>
  
  <p>Thank you.</p>