title: Linq to SQL throwing a StackOverflowException
body: <p>I'm executing a pretty simple query using Linq to SQL. I'm creating the expression and then passing it to the Where() extension method. The Linq internals are throwing a StackOverflowException when I attempt to actually execute the query. Here is the code:</p>
  
  <pre><code>int expectedCount = 4;
  Expression&lt;Func&lt;Thing, bool&gt;&gt; expression = ...;
  
  //Expression looks like (LocaleID = 1 &amp;&amp; GenderID ==1 &amp;&amp; (TimeFrameID == 2007 || TimeFrameID == 2008))
  
  using (XYZDataContext context = new XYZDataContext())
  {
      int count = context.Things.Where(expression).Count();
      //...
  }
  </code></pre>
  
  <p>And here is the DebugView of the expression:</p>
  
  <pre><code>.Lambda #Lambda1&lt;System.Func`2[XYZ.DataAccess.Thing,System.Boolean]&gt;(XYZ.DataAccess.Thing $o)
  {
      .Invoke (.Lambda #Lambda2&lt;System.Func`2[XYZ.DataAccess.Thing,System.Boolean]&gt;)($o) &amp; .Invoke (.Lambda #Lambda3&lt;System.Func`2[XYZ.DataAccess.Thing,System.Boolean]&gt;)($o)
  }
  
  .Lambda #Lambda2&lt;System.Func`2[XYZ.DataAccess.Thing,System.Boolean]&gt;(XYZ.DataAccess.Thing $o)
  {
      .Invoke (.Lambda #Lambda4&lt;System.Func`2[XYZ.DataAccess.Thing,System.Boolean]&gt;)($o) &amp; .Invoke (.Lambda #Lambda5&lt;System.Func`2[XYZ.DataAccess.Thing,System.Boolean]&gt;)($o)
  }
  
  .Lambda #Lambda3&lt;System.Func`2[XYZ.DataAccess.Thing,System.Boolean]&gt;(XYZ.DataAccess.Thing $o)
  {
      .Invoke (.Lambda #Lambda6&lt;System.Func`2[XYZ.DataAccess.Thing,System.Boolean]&gt;)($o) | .Invoke (.Lambda #Lambda7&lt;System.Func`2[XYZ.DataAccess.Thing,System.Boolean]&gt;)($o)
  }
  
  .Lambda #Lambda4&lt;System.Func`2[XYZ.DataAccess.Thing,System.Boolean]&gt;(XYZ.DataAccess.Thing $o)
  {
      $o.LocaleID == .Constant&lt;System.Nullable`1[System.Int32]&gt;(1)
  }
  
  .Lambda #Lambda5&lt;System.Func`2[XYZ.DataAccess.Thing,System.Boolean]&gt;(XYZ.DataAccess.Thing $o)
  {
      $o.GenderID == .Constant&lt;System.Nullable`1[System.Int32]&gt;(1)
  }
  
  .Lambda #Lambda6&lt;System.Func`2[XYZ.DataAccess.Thing,System.Boolean]&gt;(XYZ.DataAccess.Thing $o)
  {
      $o.TimeframeID == .Constant&lt;System.Nullable`1[System.Int32]&gt;(2007)
  }
  
  .Lambda #Lambda7&lt;System.Func`2[XYZ.DataAccess.Thing,System.Boolean]&gt;(XYZ.DataAccess.Thing $o)
  {
      $o.TimeframeID == .Constant&lt;System.Nullable`1[System.Int32]&gt;(2008)
  }
  </code></pre>
  
  <p>The expression appears correct to me and it is fairly trivial. When I read the debug view I see:</p>
  
  <pre><code>((LocaleID == 1 &amp;&amp; GenderID == 1) &amp;&amp; (TimeFrameID == 2007 || TimeFrameID == 2008))
  </code></pre>
  
  <p>...which is correct.</p>
  
  <h3>Update 1</h3>
  
  <p>Removing <strong>one</strong> of the inner or'd clauses, it works fine. So having both inner or'd clauses is breaking the translation from LINQ to SQL, somehow.</p>
  
  <h3>Update 2</h3>
  
  <p>I'm having trouble getting the debugger to step into .NET Framework code - I've tried using Reflector to do it as well as just Visual Studio. I got in once but in general stepping in is not working. The one time I did get in the StackOverflowException was occurring in:</p>
  
  <pre><code>ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, object state, bool ignoreSyncCtx)
  </code></pre>
  
  <h3>Update 3</h3>
  
  <p>Here is the code that is used to create the Expression. There is way too much code to post but the heart of it is below. I have classes which allow me to build a complex multi-level query and serialize it to JSON and XML. At the core, each piece of the query is built using the following methods and then is Or'd and And'd together:</p>
  
  <pre><code>public class LinqSearchField&lt;T, V&gt; : ISearchField
  {
      public string Name { get; private set; }
      public Expression&lt;Func&lt;T, V&gt;&gt; Selector { get; private set; }
  
      public Expression&lt;Func&lt;T, bool&gt;&gt; LessThan(V value)
      {
          return Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(Expression.LessThan(this.Selector.Body, GetConstant(value)), this.Selector.Parameters);
      }
  
      public Expression&lt;Func&lt;T, bool&gt;&gt; LessThanOrEqual(V value)
      {
          return Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(Expression.LessThanOrEqual(this.Selector.Body, GetConstant(value)), this.Selector.Parameters);
      }
  
      public Expression&lt;Func&lt;T, bool&gt;&gt; Equal(V value)
      {
          return Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(Expression.Equal(this.Selector.Body, GetConstant(value)), this.Selector.Parameters);
      }
  
      public Expression&lt;Func&lt;T, bool&gt;&gt; NotEqual(V value)
      {
          return Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(Expression.NotEqual(this.Selector.Body, GetConstant(value)), this.Selector.Parameters);
      }
  
      public Expression&lt;Func&lt;T, bool&gt;&gt; GreaterThan(V value)
      {
          return Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(Expression.GreaterThan(this.Selector.Body, GetConstant(value)), this.Selector.Parameters);
      }
  
      public Expression&lt;Func&lt;T, bool&gt;&gt; GreaterThanOrEqual(V value)
      {
          return Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(Expression.GreaterThanOrEqual(this.Selector.Body, GetConstant(value)), this.Selector.Parameters);
      }
  
      private ConstantExpression GetConstant(V value)
      {
          return Expression.Constant(value, typeof(V));
      }
  
      public Expression&lt;Func&lt;T, bool&gt;&gt; Null()
      {
          return Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(Expression.Equal(this.Selector.Body, Expression.Constant(null)), this.Selector.Parameters);
      }
  
      public Expression&lt;Func&lt;T, bool&gt;&gt; NotNull()
      {
          return Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(Expression.NotEqual(this.Selector.Body, Expression.Constant(null)), this.Selector.Parameters);
      }
  }
  </code></pre>
  
  <p>Here is the And code (the OR code is the same but with Expression.And instead):</p>
  
  <pre><code>public static Expression&lt;Func&lt;T, bool&gt;&gt; And&lt;T&gt;(this Expression&lt;Func&lt;T, bool&gt;&gt; expression1, Expression&lt;Func&lt;T, bool&gt;&gt; expression2)
  {
      ParameterExpression[] parameters = expression1.Parameters.Union(expression2.Parameters).Distinct(new ParameterExpressionComparer()).ToArray();
      InvocationExpression invocationExpression1 = Expression.Invoke(expression1, parameters);
      InvocationExpression invocationExpression2 = Expression.Invoke(expression2, parameters);
      Expression binaryExpression = null;
  
      //And the current expression to the previous one.
      binaryExpression = Expression.AndAlso(invocationExpression1, invocationExpression2); //Or OrElse.
  
      //Wrap the expression in a lambda.
      return Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(binaryExpression, parameters);
  }
  </code></pre>
  
  <h3>Update 4</h3>
  
  <p>It will probably be frowned upon but here is a <a href="http://cid-0ede5d21bdc5f270.office.live.com/self.aspx/Public/Forums/StackOverflow/LinqToSQLTest.zip">sample which reproduces this issue</a>. I really need to figure out what's going on here.</p>