c# - 无法为基本属性翻译 LINQ 表达式

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

我有自定义的 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]>))

最佳答案

我以前见过这样的事情。编译器生成的表达式和手动表达式之间的唯一区别是 PropertyInfoReflectedType 属性 - 在编译器生成的代码中,它与 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/

相关文章:

c# - 如何使用 .NET Core 将 Base64 图像上传到 firebase

c# - .NET Core 控制台应用依赖注入(inject)设置

c# - Azure Active Directory 抛出 "Not implemented"用于 api 管理灾难恢复

c# - 如何在 EF 中获取重复的嵌套实体?

c# - EF Core 在迁移 (UP) 时将列数据从表移动到另一个表

c# - Cuda - OpenCL CPU 比 OpenCL 或 CUDA GPU 版本快 4 倍

c# - 在没有边框的 native 浏览器中调整 html 滚动条的大小

c# - 应用程序长时间空闲后,EF Core AddAsync 和 SaveChangesAsync 稍微变慢

c# - EF 迁移不会自动更新数据库

c# - C# 代码语法中的 "s"和 "e"是什么