title: NotSupportedException using ToList() in LINQ and EntityFramework
body: <p>I have a query that looks like this:</p>
  
  <pre><code>    var q = from x in db.Table1
                  where x.Timestamp.CompareTo(fromDate) &gt;= 0
                     &amp;&amp; x.Timestamp.CompareTo(toDate) &lt; 0
                  select x;
  
      var q_list = q.ToList(); // this works fine
      var g = q.Where(z=&gt; z.Table2.Equals(ns));  // ns is instance of Table2 not Table1
      var g_list = g.ToList(); // this throws exception 
  </code></pre>
  
  <p>The exception:</p>
  
  <p>**</p>
  
  <pre><code>System.NotSupportedException was unhandled
    Message=Unable to create a constant value of type ...
  </code></pre>
  
  <p>I have a lot of functions like that and I do not want to repeat queries, I need to use the Where, GroupBy ...etc and other functions.</p>
  
  <p>FULL exception trace:</p>
  
  <p>**</p>
  
  <pre><code>System.NotSupportedException was unhandled
    Message=Unable to create a constant value of type 'DataAccessLayer.Model.Table2. Only primitive types ('such as Int32, String, and Guid') are supported in this context.
    Source=System.Data.Entity
    StackTrace:
         at System.Data.Objects.ELinq.ExpressionConverter.ConstantTranslator.TypedTranslate(ExpressionConverter parent, ConstantExpression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.EqualsTranslator.TypedTranslate(ExpressionConverter parent, BinaryExpression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input)
         at System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input, DbExpressionBinding&amp; binding)
         at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression&amp; source, DbExpressionBinding&amp; sourceBinding, DbExpression&amp; lambda)
         at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)
         at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod)
         at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
         at System.Data.Objects.ELinq.ExpressionConverter.Convert()
         at System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)
         at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
         at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable&lt;T&gt;.GetEnumerator()
         at System.Data.Entity.Internal.Linq.InternalQuery`1.GetEnumerator()
         at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable&lt;TResult&gt;.GetEnumerator()
         at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
  </code></pre>
  
  <p>**</p>