PredicateBuilder< True>之间的差异和PredicateBuilder< False> ;? [英] Difference between PredicateBuilder<True> and PredicateBuilder<False>?
问题描述
我有代码:
var predicate = PredicateBuilder.True<Value>();
predicate = predicate.And(x => x.value1 == "1");
predicate = predicate.And(x => x.value2 == "2");
var vals = Value.AsExpandable().Where(predicate).ToList();
如果我有 PredicateBuilder.True< Value>()
,它会带回我期望的值,但是如果我有 PredicateBuilder.False< Value>()
,它会带回0条记录。有人可以解释一下区别是什么,为什么在一种情况下我得到0条记录,而在另一种情况下我得到了我期望的结果。我已经阅读了 PredicateBuilder
的文档,但这有点令人困惑。我觉得这与我 Anding
谓词在一起?
If I have PredicateBuilder.True<Value>()
, it brings back what I expect, but if I have PredicateBuilder.False<Value>()
, it brings back 0 records. Can someone explain what the the difference is and why in one scenario I get back 0 records an in the other I get what I expect. I already read the PredicateBuilder
documenation, but it was a bit confusing. I have a feeling it has to do with the fact that I am Anding
predicates together?
推荐答案
使用 PredicateBuilder
递增地构建0个或更多条件的谓词时,从可以添加到中性谓词开始的位置很方便,因为您可以随后遍历条件,然后将和
或或
与最后一个条件一起使用。例如,
When using PredicateBuilder
to incrementally build a predicate of 0 or more conditions, it is convenient to start off with a "neutral" predicate that can be added to, since you can then just iterate through the conditions and either and
or or
them together wth the last one. E.g.
var predicate = PredicateBuilder.True<Value>();
foreach (var item in itemsToInclude) {
predicate = predicate.And(o => o.Items.Contains(item));
}
这等效于更简单的布尔逻辑:
This would be equivalent to the more straightforward boolean logic:
var predicate = true;
foreach (var item in itemsToInclude) {
predicate = predicate && o.Items.Contains(item);
}
这将等同于
true && ((o.Items.Contains(itemsToInclude[0] && o.Items.Contains.itemsToInclude[1]) ...)
或 true& restOfPredicate
,如果 restOfPredicate
是true,如果 restOfPredicate
是<$ c,则 false
Or true && restOfPredicate
, which evaluates to true
if restOfPredicate
is true, and false
if restOfPredicate
is false
. Hence why it's considered neutral.
从 PredicateBuilder.False $ c $开始c>,但是等同于
false& restOfPredicate
,该值始终会评估为 false
。
Starting out with PredicateBuilder.False
, however, would be equivalent false && restOfPredicate
, which would always evaluate to false
.
类似地,对于或
,以 false
开头到 false || restOfPredicate
,如果 restOfPredicate
false 如果 restOfPredicate
是<$ c,则>为 false
和 true
$ c> true 。而 true || restOfPredicate
始终将评估为 true
。
Similarly for or
, starting out with false
would be equivalent to false || restOfPredicate
, which evaluate to false
if restOfPredicate
is false
and true
if restOfPredicate
is true
. And true || restOfPredicate
would always evaluate to true
.
底线:使用 PredicateBuilder.True
作为中性起点,并使用 PredicateBuilder.And
和 PredicateBuilder.False
与 PredicateBuilder.Or
。
Bottom line: Use PredicateBuilder.True
as a neutral starting point with PredicateBuilder.And
, and PredicateBuilder.False
with PredicateBuilder.Or
.
这篇关于PredicateBuilder< True>之间的差异和PredicateBuilder< False> ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!