title: How to filter date in server side using dynamic linq?
body: <p>I want to filter dates range. I'm using the dynamic LINQ library. I have to build my where string into a variable based on several factors and then pass the string variable to the where clause.</p>
  
  <p>In my ASP.NET MVC project I wrote a helper.</p>
  
  <pre><code>private string GetFilterCondition(string filterCondition, string filterDataField, string filterValue)
  {
          switch (filterCondition)
          {
  
              case "CONTAINS":
                  return " " + filterDataField + ".Contains(\"" + filterValue + "\")";
              case "GREATER_THAN_OR_EQUAL":
                  return " " + filterDataField + " &gt;= " +"\"" + filterValue + "\"";
              case "LESS_THAN_OR_EQUAL":
                  return " " + filterDataField + " &lt;= " + "\"" + filterValue + "\"";             
          }
          return "";
  }
  </code></pre>
  
  <p>And I used parameter in query which I've passed from the ASP.NET MVC project;</p>
  
  <pre><code>public int GetFilteredCount(string searchValue)
  {
          var filteredRecords = 0;
  
          try               
          {                   
               var query = DB.Categories.AsQueryable();
               var totalRecords = query.Count();
  
               if (!string.IsNullOrEmpty(searchValue))
               {                    
                   searchValue = searchValue.Trim();        
                   query = query.Where(searchValue);
               }
  
               filteredRecords = query.Count();                
  
           }
           catch (Exception e){}
           finally
           {
               DestroyDbContext();
           }
  
           return result;
  }
  </code></pre>
  
  <p>When filtering date range ("GREATER_THAN_OR_EQUAL", "LESS_THAN_OR_EQUAL"), I get an error:</p>
  
  <blockquote>
    <p>Operator '>=' incompatible with operand types 'DateTime' and 'String'</p>
  </blockquote>
  
  <p>Please note, that with 'contains' works.</p>
  
  <p>I uploaded picture to see what looks like searchvalue parameter</p>
  
  <p><a href="https://i.stack.imgur.com/RG8Sy.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RG8Sy.jpg" alt="enter image description here"></a></p>
  
  <p>I rewrote GetFilterCondition function like this:</p>
  
  <pre><code>case "GREATER_THAN_OR_EQUAL":
     return string.Format(" {0} &gt;= DateTime.Parse(\"{1}\")", filterDataField, filterValue);
  
  case "LESS_THAN_OR_EQUAL":
     return string.Format(" {0} &lt;= DateTime.Parse(\"{1}\")", filterDataField, filterValue);
  </code></pre>
  
  <p>after executed filteredRecords = query.Count() throws this error:</p>
  
  <blockquote>
    <p>LINQ to Entities does not recognize the method 'System.DateTime Parse(System.String)' method, and this method cannot be translated into a store expression</p>
  </blockquote>