我有自定义的 OrderBy 实现,它只适用于没有继承的类型,如果我想从我得到的基类型中按字段排序,我得到了 LINQ 表达式无法翻译
public static IOrderedQueryable<TEntity> OrderBy<TEntity>(this IQueryable<TEntity> source, string orderByProperty, bool desc)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (string.IsNullOrEmpty(orderByProperty))
{
throw new ArgumentNullException(nameof(orderByProperty));
}
var command = desc ? "OrderByDescending" : "OrderBy";
var type = typeof(TEntity);
var param = Expression.Parameter(type, "p");
var property = type.GetProperty(orderByProperty);
var propertyAccess = Expression.MakeMemberAccess(param, property);
var orderByExpression = Expression.Lambda(propertyAccess, param);
var resultExpression = Expression.Call(
typeof(Queryable),
command,
new Type[] { type, property.PropertyType },
source.Expression,
Expression.Quote(orderByExpression));
return (IOrderedQueryable<TEntity>)source.Provider.CreateQuery(resultExpression);
}
我正在使用 entityframework core 2.2,但非常有趣的是,如果我只写 source.OrderBy(x=>x.someBaseField)
那么它可以毫无问题地工作,所以必须有一些东西使用我的自定义实现
在错误日志中我也得到了翻译后的查询,它看起来像这样,有趣的是结束部分
orderby new SomeType() {NewField = [entity].DbField, Id = [entity].Id}.Id desc
orderByExpression.Body {p => p.Id}
resultExpression
.Call System.Linq.Queryable.OrderByDescending(
.Call System.Linq.Queryable.Select(
.Call System.Linq.Queryable.Where(
.Call System.Linq.Queryable.Where(
.Constant<Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[MyTypeView]>(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[MyTypeView]),
'(.Lambda #Lambda1<System.Func`2[MyTypeView,System.Boolean]>)),
'(.Lambda #Lambda2<System.Func`2[MyTypeView,System.Boolean]>)),
'(.Lambda #Lambda3<System.Func`2[MyTypeView, MyTypeResult]>))
,
'(.Lambda #Lambda4<System.Func`2[MyTypeResult,System.Guid]>))
最佳答案
我以前见过这样的事情。编译器生成的表达式和手动表达式之间的唯一区别是 PropertyInfo
的 ReflectedType
属性 - 在编译器生成的代码中,它与 DeclaringType
相同,它在this case是基类,而在通过type.GetProperty
获取的PropertyInfo
中是用来获取它的派生类型。
由于某些未知原因(可能是错误),这让 EF Core 感到困惑。解决方法是更改代码如下:
var property = type.GetProperty(orderByProperty);
if (property.DeclaringType != property.ReflectedType)
property = property.DeclaringType.GetProperty(property.Name);
或者使用像这样的辅助方法
static PropertyInfo GetProperty(Type type, string name)
{
for (; type != null; type = type.BaseType)
{
var property = type.GetProperty(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
if (property != null) return property;
}
return null;
}
为了支持嵌套属性,我会添加以下助手
static Expression Property(Expression target, string name) =>
name.Split('.').Aggregate(target, SimpleProperty);
static Expression SimpleProperty(Expression target, string name) =>
Expression.MakeMemberAccess(target, GetProperty(target.Type, name));
然后使用
var propertyAccess = Property(param, orderByProperty);
和
new Type[] { type, orderByExpression.ReturnType },
在有问题的方法中。
关于c# - 无法为基本属性翻译 LINQ 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55295009/