4
使用ToList()我有一个查询,看起来像这样:引发NotSupportedException在LINQ和的EntityFramework
var q = from x in db.Table1
where x.Timestamp.CompareTo(fromDate) >= 0
&& x.Timestamp.CompareTo(toDate) < 0
select x;
var q_list = q.ToList(); // this works fine
var g = q.Where(z=> z.Table2.Equals(ns)); // ns is instance of Table2 not Table1
var g_list = g.ToList(); // this throws exception
例外:
**
System.NotSupportedException was unhandled
Message=Unable to create a constant value of type ...
我有很多的功能像那样,我不想重复查询,我需要使用Where,GroupBy等等等功能。
FULL异常跟踪:
**
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& binding)
at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression& source, DbExpressionBinding& sourceBinding, DbExpression& 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<T>.GetEnumerator()
at System.Data.Entity.Internal.Linq.InternalQuery`1.GetEnumerator()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
**
好,'.ToList()'负责例外,只是因为它是一个方法(其中许多,如'.ToArray()','.Single()'等)实际上会“评估”LINQ表达式。在那之前,它甚至不会触及数据库。在那个时候,有很多事情正在进行,显然,其中的一部分正在抛出这个例外。你可以发布完整的异常/堆栈跟踪吗? – 2012-07-20 02:37:28
感谢@Saher发布堆栈跟踪。那么现在,'z.field'的缺点是什么?什么是'field'? – 2012-07-20 02:47:56
然后ivowiblo是一个优秀的心灵读者。他钉住了答案。 ;) – 2012-07-20 02:53:43