title: Creating an Expression Tree with !DateTime.HasValue
body: <p>I have a very large expression tree builder that has been built and added to regularly as new functionality was needed. It has been working perfectly for all types including Nullable&lt;> types. Several of my Nullable&lt;> types are DateTime as those columns in my database allow nulls.</p>
  
  <p>Now, I need to adjust the method to be able to filter based on a DateTime having ANY value. In SQL terms:</p>
  
  <pre><code>WHERE date_Column IS NOT NULL
  </code></pre>
  
  <p>In LINQ terms:</p>
  
  <pre><code>.Where(s =&gt; !s.date_column.HasValue)
  </code></pre>
  
  <p>For the life of me, I cannot figure out how to add this to my expression tree. I can add any operator to the MyFilter class that I like, so if a new case will help, that is no problem.</p>
  
  <p>Below is the builder cut down to just the DateTime portion for readability.</p>
  
  <pre><code>public class MyFilter
  {
      public string FieldName { get; set; }
      public string FieldValue { get; set; }
      public string Operator { get; set; }
  }
  
  private Expression&lt;Func&lt;MyDTO, bool&gt;&gt; CreateLambda(MyFilter myFilter)
  {
      ParameterExpression parameter = Expression.Parameter(typeof(MyDTO), "m");
      Expression property = Expression.Property(parameter, myFilter.FieldName);
      Expression target = null;
      Expression exp = null;
      PropertyInfo pi = null;
      MethodInfo mi = null;
  
      var switchType = property.Type.ToString();
  
      switch (switchType)
      {
          case "System.DateTime":
              target = (myFilter.FieldValue == "null") ?
                  Expression.Constant(null, property.Type) :
                  Expression.Constant(Convert.ToDateTime(myFilter.FieldValue));
              switch (myFilter.Operator)
              {
                  case "eq":
                      exp = Expression.Equal(property, Expression.Convert(target, property.Type));
                      break;
                  case "ne":
                      exp = Expression.NotEqual(property, Expression.Convert(target, property.Type));
                      break;
                  case "ge":
                      exp = Expression.GreaterThanOrEqual(property, Expression.Convert(target, property.Type));
                      break;
                  case "gt":
                      exp = Expression.GreaterThan(property, Expression.Convert(target, property.Type));
                      break;
                  case "le":
                      exp = Expression.LessThanOrEqual(property, Expression.Convert(target, property.Type));
                      break;
                  case "lt":
                      exp = Expression.LessThan(property, Expression.Convert(target, property.Type));
                      break;
              }
              break;
      }
      Expression&lt;Func&lt;MyDTO, bool&gt;&gt; lambda = Expression.Lambda&lt;Func&lt;MyDTO, bool&gt;&gt;(exp, parameter);
      return lambda;
  }
  </code></pre>