带有“NOT IN”的 C# 谓词生成器功能性

发布于 2024-12-01 04:28:23 字数 398 浏览 3 评论 0原文

使用 PredicateBuilder 如何获得类似于 SQL IN 或 NOT IN 查询的功能?

例如,我有一个 ID 列表,我想选择 ID 匹配或不匹配的所有人员。

人员匹配功能相当简单(尽管可能有更好的方法)

var predicate = PredicateBuilder.False<Person>()
foreach (int i in personIDs)
{
  int temp = i;
  predicate = predicate.Or(e=>e.PersonID == temp);
}
return persons.Where(predicate);

那么我如何得到相反的结果呢?我想要所有 ID 不在 personID 列表中的人。

With PredicateBuilder how do I get functionality similar to the SQL IN or NOT IN query?

For example I have a list of IDs and I want to select all of the People whose IDs either Match or do not match the IDs.

The people match functionality is fairly straightforward (although there may be a better way to do it)

var predicate = PredicateBuilder.False<Person>()
foreach (int i in personIDs)
{
  int temp = i;
  predicate = predicate.Or(e=>e.PersonID == temp);
}
return persons.Where(predicate);

So how do I get the opposite? I want all persons whose IDs are not in the personIDs list.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

挖个坑埋了你 2024-12-08 04:28:24

你使用实体框架吗?

然后,您可以在没有 PredicateBuilder 的情况下构建查询:

var personIds = new List<int>() { 8,9,10 };
var query = persons.Where(it => !personIds.Contains(it.PersonId));

从此 LINQ 语句创建 SQL NOT IN 查询。

Do you use Entity Framework?

Then you can build the query without PredicateBuilder:

var personIds = new List<int>() { 8,9,10 };
var query = persons.Where(it => !personIds.Contains(it.PersonId));

From this LINQ statement a SQL NOT IN query is created.

┼── 2024-12-08 04:28:24

这是你想要的吗?

var predicate = PredicateBuilder.True<Person>()
foreach (int i in personIDs)
{
    int temp = i;
    predicate = predicate.And(e => e.PersonID != temp);
}
return persons.Where(predicate);

Is this what you want?

var predicate = PredicateBuilder.True<Person>()
foreach (int i in personIDs)
{
    int temp = i;
    predicate = predicate.And(e => e.PersonID != temp);
}
return persons.Where(predicate);
无敌元气妹 2024-12-08 04:28:24

不看api....

var predicate = PredicateBuilder.True<Person>()
foreach (int i in personIDs)
{
    int temp = i;
    predicate = predicate.And(e=>e.PersonID != temp);
}
return persons.Where(predicate);

Without looking at the api....

var predicate = PredicateBuilder.True<Person>()
foreach (int i in personIDs)
{
    int temp = i;
    predicate = predicate.And(e=>e.PersonID != temp);
}
return persons.Where(predicate);
回忆凄美了谁 2024-12-08 04:28:23

询问De Morgan

非(P 或 Q)=(非 P)且(非 Q)

要让您的代码生成与 NOT IN 条件等效的内容,请重写为

var predicate = PredicateBuilder.True<Person>()

and

predicate = predicate.And(e=>e.PersonID != temp);

Ask De Morgan:

NOT (P OR Q) = (NOT P) AND (NOT Q)

To have your code generate the equivalent of a NOT IN condition, rewrite as

var predicate = PredicateBuilder.True<Person>()

and

predicate = predicate.And(e=>e.PersonID != temp);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文