EF Core 3和EF 6之间的区别-无法解析基于表达式的OrderBy?

时间:2019-11-25 10:54:33

标签: c# .net-core entity-framework-core entity-framework-core-3.0

我必须在IQueryable 上动态构建OrderBy。以下内容可用于EF 6:

IQueryable<T> query = ...
var parameter = Expression.Parameter(query.ElementType, "p");
var property = Expression.Property(parameter, "NameOfProperty");
var getProperty = Expression.Lambda(property, new [] { parameter });

var expression = Expression.Call(typeof(Queryable), nameof(Queryable.OrderBy), 
new [] { query.ElementType, typeof(TKey) }, 
new [] { query.Expression, Expression.Quote(getProperty) });

query = query.Provider.CreateQuery<T>(expression);

// And finally
var results = await query.Take(x).ToListAsync();

从理论上讲,这构建了以下内容:

query = query.OrderBy(p => p.NameOfProperty);

在EF Core 3中执行前者会导致InvalidOperationExpression:

The LINQ expression 'OrderBy<T, TKey>(
…
keySelector: (p) => p.NameOfProperty' could not be translated... switch to client evaluation...

我试图比较这两种情况下的树,它们看起来是相同的(至少从文本表示来看),但是翻译仍然失败。我想念什么吗?

Core 3 RTM / W Microsoft SQL Server 2019

0 个答案:

没有答案