我必须在IQueryable
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