gpt4 book ai didi

c# - NotSupportedException 在 LINQ 和 EntityFramework 中使用 ToList()

转载 作者:太空狗 更新时间:2023-10-30 00:20:28 24 4
gpt4 key购买 nike

我有一个如下所示的查询:

    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 ...等功能。

完整异常跟踪:

**

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)

**

最佳答案

问题是 EntityFramework 不理解非原始类型。你要做的是:

var g = q.Where(z => z.field.Id.Equals(ns.Id)); 

请记住,如果您有 Equals 的自定义实现,则无法将其转换为正确的 t-sql。如果您无论如何都想执行该实现,则需要在内存中执行:

// note it's using q_list instead of q
var g = q_list.Where(z => z.field.Equals(ns));

关于c# - NotSupportedException 在 LINQ 和 EntityFramework 中使用 ToList(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11572064/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com