与 EF core 中的不同(表达式树)

som*_*men 1 c# entity-framework-core ef-core-6.0

我正在动态构建查询,但在实现NOT LIKE. 我的LIKE看起来像这样:

case "LIKE":
{
    var memberTypeConverter = TypeDescriptor.GetConverter(member.Type);
    var constant = Expression.Constant(value == null ? null : memberTypeConverter.ConvertFrom(value.ToString()!), member.Type);

    body = Expression.Call(
        typeof(DbFunctionsExtensions),
        nameof(DbFunctionsExtensions.Like),
        Type.EmptyTypes,
        Expression.Property(null, typeof(EF), nameof(EF.Functions)),
        member,
        constant
    );
    break;
}
Run Code Online (Sandbox Code Playgroud)

这是可行的,但是没有DbFunctionsExtensions.NotLike或类似的东西,而且我不知道如何否定Like.

我尝试使用类似的东西IsFalse,但没有用。

body = Expression.IsFalse(Expression.Call(
                        typeof(DbFunctionsExtensions),
                        nameof(DbFunctionsExtensions.Like),
                        Type.EmptyTypes,
                        Expression.Property(null, typeof(EF), nameof(EF.Functions)),
                        member,
                        constant
                        )`
...
Run Code Online (Sandbox Code Playgroud)

如何获得计算结果为NOT LIKE查询的表达式?我怎样才能NOT LIKE在普通纸上写字Queryable

Dav*_*idG 6

您只需要将表达式包装在 an 中Expression.Not即可反转逻辑。所以可能是这样的:

...

body = Expression.Call(
    typeof(DbFunctionsExtensions),
    nameof(DbFunctionsExtensions.Like),
    Type.EmptyTypes,
    Expression.Property(null, typeof(EF), nameof(EF.Functions)),
    member,
    constant
);

body = Expression.Not(body); // <--- Add this
Run Code Online (Sandbox Code Playgroud)