linq ExecuteUpdateAsync的链接表达式

72qzrwbm  于 2022-12-06  发布在  其他
关注(0)|答案(2)|浏览(228)

我很热情地在EF Core 7中找到了ExecuteDeleteAsync和ExecuteUpdateAsync,它们帮助我的代码变得更简单、更快,不需要使用自制的过程来批量删除或更新1-2个字段,反正我也有在运行时需要选择数据库的确切表和字段进行更新的情况。
我可以使用数据库表:

public static IQueryable<object> Set(this DbContext context, Type entity) =>
        context.ClrQuery(context.ClrType(entity));

我有方法让表达式过滤行:

public static IQueryable Where(this IQueryable source, string equalProperty, object value, [NotNull] Type EntityType)
{
    PropertyInfo? property = EntityType.GetProperty(equalProperty);
    if (property == null)
        throw new NotImplementedException($"Type {EntityType.Name} does not contain property {equalProperty}");

    ParameterExpression parameter = Expression.Parameter(EntityType, "r");
    MemberExpression member = Expression.MakeMemberAccess(parameter, property);
    LambdaExpression whereExpression = Expression.Lambda(Expression.Equal(member, Expression.Constant(value, property.PropertyType)), parameter);
    MethodCallExpression resultExpression = WhereCall(source, whereExpression);
    return source.Provider.CreateQuery(resultExpression);
}

因此,我可以使用以下命令查找要更新的行

IQueryable Source = db.Set(EntityType).Where(FieldName, FieldValue, EntityType);

我应该创建表达式来更新IQueryable ExecuteUpdateQuery = Source.ExecuteUpdateAsync(EntityType, FieldName, FieldValue);
访问SetProperty表达式的方法是什么?

0ve6wy6x

0ve6wy6x1#

尝试以下扩展。我还更正了方法签名:

var affected = anyQuery.ExecuteUpdate(FieldName, FieldValue);

var affected = await anyQuery.ExecuteUpdateAsync(FieldName, FieldValue, cancellationToken);

并实现:

public static class DynamicRelationalExtensions
{
    static MethodInfo UpdateMethodInfo =
        typeof(RelationalQueryableExtensions).GetMethod(nameof(RelationalQueryableExtensions.ExecuteUpdate));

    static MethodInfo UpdateAsyncMethodInfo =
        typeof(RelationalQueryableExtensions).GetMethod(nameof(RelationalQueryableExtensions.ExecuteUpdateAsync));

    public static int ExecuteUpdate(this IQueryable query, string fieldName, object? fieldValue)
    {
        var updateBody = BuildUpdateBody(query.ElementType, fieldName, fieldValue);

        return (int)UpdateMethodInfo.MakeGenericMethod(query.ElementType).Invoke(null, new object?[] { query, updateBody });
    }

    public static Task<int> ExecuteUpdateAsync(this IQueryable query, string fieldName, object? fieldValue, CancellationToken cancellationToken = default)
    {
        var updateBody = BuildUpdateBody(query.ElementType, fieldName, fieldValue);

        return (Task<int>)UpdateAsyncMethodInfo.MakeGenericMethod(query.ElementType).Invoke(null, new object?[] { query, updateBody, cancellationToken })!;
    }

    static LambdaExpression BuildUpdateBody(Type entityType, string fieldName, object? fieldValue)
    {
        var setParam = Expression.Parameter(typeof(SetPropertyCalls<>).MakeGenericType(entityType), "s");
        var objParam = Expression.Parameter(entityType, "e");

        var propExpression = Expression.PropertyOrField(objParam, fieldName);
        var valueExpression = ValueForType(propExpression.Type, fieldValue);

        // s.SetProperty(e => e.SomeField, value)
        var setBody = Expression.Call(setParam, nameof(SetPropertyCalls<object>.SetProperty),
            new[] { propExpression.Type }, Expression.Lambda(propExpression, objParam), valueExpression);

        // s => s.SetProperty(e => e.SomeField, value)
        var updateBody = Expression.Lambda(setBody, setParam);

        return updateBody;
    }

    static Expression ValueForType(Type desiredType, object? value)
    {
        if (value == null)
        {
            return Expression.Default(desiredType);
        }

        if (value.GetType() != desiredType)
        {
            value = Convert.ChangeType(value, desiredType);
        }

        return Expression.Constant(value);
    }
}
wqsoz72f

wqsoz72f2#

谢谢,效果很好.

相关问题