如何使用Dapper Extensions谓词实现“ NOT IN”子句? [英] How to implement “NOT IN” clause with Dapper Extensions Predicate?

查看:122
本文介绍了如何使用Dapper Extensions谓词实现“ NOT IN”子句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了如何使用Dapper扩展IN 子句-clause-with-dapper-extensions-predicate>此处。

I found how to implement IN clause using Dapper Extensions here.

现在,我想实现 NOT IN 子句。

因此,我期望SQL查询如下:

Now, I want to implement NOT IN clause.
So, I am expecting SQL query something like below:

SELECT * FROM MyTable
WHERE MyField NOT IN (param1, param2)

但是我找不到关于 NOT IN 或 NOT 子句。

But I could not find anything about NOT IN or NOT clause in Dapper Extensions.

如何实现 NOT IN 子句带有Dapper Extensions谓词?

How can I implement NOT IN clause with Dapper Extensions Predicate?

推荐答案

请参考此答案(也有问题链接),以了解如何实现 IN 子句。

Please refer to this answer (linked in question as well) to understand how to implement IN clause.

要将上述的 IN 子句转换为 NOT IN 子句,请使用最后一个 bool not 参数。

这是可选参数,默认值为 false

这就是为什么;即使很明显,它也被隐藏了,因此未被发现

文档也没有明确提及它。

To turn the IN clause as mentioned above to NOT IN clause, use the last bool not parameter.
This is optional parameter and default value for it is false.
That is why; even though so obvious, it is bit hidden and hence undiscovered.
Documentation does not mention it explicitly either.

以下是Dapper Extensions源代码中定义的每个谓词的定义:

Below are the definitions of each predicate defined in Dapper Extensions source code:

public static class Predicates
{
    public static IBetweenPredicate Between<T>(Expression<Func<T, object>> expression, BetweenValues values, bool not = false) where T : class;
    public static IExistsPredicate Exists<TSub>(IPredicate predicate, bool not = false) where TSub : class;
    public static IFieldPredicate Field<T>(Expression<Func<T, object>> expression, Operator op, object value, bool not = false) where T : class;
    public static IPropertyPredicate Property<T, T2>(Expression<Func<T, object>> expression, Operator op, Expression<Func<T2, object>> expression2, bool not = false)
        where T : class
        where T2 : class;
}

示例代码如下:

var predicate = Predicates.Field<Customer>
                (f => f.CustomerID, Operator.Eq, listOfIDs, true);

观察以上代码中最后一个参数的值 true listOfIDs 是您数据类型的 IEnumerable

Observe the value true for last parameter in above code. The listOfIDs is an IEnumerable of your data type.

请参阅以获取更多源代码。

Please refer to this for more source code.

这篇关于如何使用Dapper Extensions谓词实现“ NOT IN”子句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆