带有“NOT IN”的 C# 谓词生成器功能性
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你使用实体框架吗?
然后,您可以在没有 PredicateBuilder 的情况下构建查询:
从此 LINQ 语句创建 SQL NOT IN 查询。
Do you use Entity Framework?
Then you can build the query without PredicateBuilder:
From this LINQ statement a SQL NOT IN query is created.
这是你想要的吗?
Is this what you want?
不看api....
Without looking at the api....
询问De Morgan:
要让您的代码生成与 NOT IN 条件等效的内容,请重写为
and
Ask De Morgan:
To have your code generate the equivalent of a NOT IN condition, rewrite as
and