title: Declaring literal dates in lambda expression
body: <p>I'm currently developping an simple engine which generates lamda expressions from different classes (using reflexion and expression factories).</p>
  <p>I have an issue trying to make some unit tests on my engine.</p>
  <p>I use my engine to generate a lambda expression, then I compare the generated lambda with a &quot;hand made&quot; lambda expression. The problem is when I use a DateTime in my lambda expression. The lambda generated with reflection contains an &quot;hard coded&quot; date, whereas in the &quot;hand made&quot; lambda expression, I must use a &quot;new DateTime(...)&quot;.</p>
  <p>So the unit test fails with the following message (I cut the message to keep only the interresting part) :</p>
  <blockquote>
  <p>Expected: &lt; x =&gt; (...) AndAlso (x.Date &lt;= new DateTime(2015, 1, 1)))&gt;</p>
  <p>But was:  &lt; x =&gt; (...) AndAlso (x.Date &lt;= 01/01/2015 00:00:00))&gt;</p>
  </blockquote>
  <p>My assert is the following :</p>
  <pre><code>Assert.AreEqual(expectedWhere, request.Filter);
  </code></pre>
  <p>Where &quot;expectedWhere&quot; is the &quot;hand made&quot; lambda and &quot;request.Filter&quot; is the generated lambda.</p>
  <hr />
  <p>Is there a solution (<a href="https://msdn.microsoft.com/en-us/library/aa711653(v=vs.71).aspx" rel="nofollow noreferrer">like in VB.NET</a>) to create my lambda expression like that :</p>
  <pre><code>Expression&lt;Func&lt;Model, bool&gt;&gt; expectedWhere = x =&gt; (...) &amp;&amp; (x.Date &lt;= [01/01/2015 00:00:00])
  </code></pre>
  <p>I already test to use an external variable, DateTime.Now, or a constant (not managed by the .NET framework).</p>
  <hr />
  <h2>EDIT :</h2>
  <p>This is a simple fiddle showing my problem : <a href="https://dotnetfiddle.net/fKccY3" rel="nofollow noreferrer">https://dotnetfiddle.net/fKccY3</a></p>
  <p>Code :</p>
  <pre><code>ParameterExpression param = Expression.Parameter(typeof(Model), &quot;x&quot;);
  var property = Expression.Property(param, &quot;Date&quot;);
  ConstantExpression constant = Expression.Constant(new DateTime(2015, 1, 1), typeof(DateTime));
  
  Expression finalExpression = Expression.LessThanOrEqual(property, constant);
  
  var tree = Expression.Lambda&lt;Func&lt;Model, bool&gt;&gt;(finalExpression, param);
  Console.WriteLine(tree);
  
  Expression&lt;Func&lt;Model, bool&gt;&gt; handMaidExpression = x =&gt; x.Date == new DateTime(2015, 1, 1);
  
  Console.WriteLine(handMaidExpression);
  </code></pre>