[英]Difference between PredicateBuilder<True> and PredicateBuilder<False>?
I have the code: 我有代码:
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();
If I have PredicateBuilder.True<Value>()
, it brings back what I expect, but if I have PredicateBuilder.False<Value>()
, it brings back 0 records. 如果我有
PredicateBuilder.True<Value>()
,它会带回我的期望,但如果我有PredicateBuilder.False<Value>()
,它会带回0条记录。 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. 有人可以解释这是什么区别,为什么在一个场景中我得到0记录在另一个我得到我期望的。 I already read the
PredicateBuilder
documenation, but it was a bit confusing. 我已经阅读了
PredicateBuilder
文档,但它有点令人困惑。 I have a feeling it has to do with the fact that I am Anding
predicates together? 我有一种感觉,这与我和
Anding
谓词在一起的事实有关吗?
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. 当使用
PredicateBuilder
逐步构建0或更多条件的谓词时,可以方便地从可以添加的“中性”谓词开始,因为您可以只是迭代条件,或者或者and
/ or
它们一起使用最后一个。 Eg 例如
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);
}
Which would be equivalent to 这相当于
true && ((o.Items.Contains(itemsToInclude[0] && o.Items.Contains.itemsToInclude[1]) ...)
Or true && restOfPredicate
, which evaluates to true
if restOfPredicate
is true, and false
if restOfPredicate
is false
. 或者
true && restOfPredicate
,如果restOfPredicate
为true,则计算结果为true
如果restOfPredicate
为false
则restOfPredicate
为false
。 Hence why it's considered neutral. 因此,为什么它被认为是中立的。
Starting out with PredicateBuilder.False
, however, would be equivalent false && restOfPredicate
, which would always evaluate to false
. 然而,从
PredicateBuilder.False
开始,将等同于false && restOfPredicate
,它总是评估为false
。
Similarly for or
, starting out with false
would be equivalent to false || restOfPredicate
类似地,
or
,以false
开头将等同于false || restOfPredicate
false || restOfPredicate
, which evaluate to false
if restOfPredicate
is false
and true
if restOfPredicate
is true
. false || restOfPredicate
,其计算结果为false
,如果restOfPredicate
是false
和true
如果restOfPredicate
是true
。 And true || restOfPredicate
true || restOfPredicate
true || restOfPredicate
would always evaluate to true
. true || restOfPredicate
将始终评估为true
。
Bottom line : Use PredicateBuilder.True
as a neutral starting point with PredicateBuilder.And
, and PredicateBuilder.False
with PredicateBuilder.Or
. 底线 :使用
PredicateBuilder.True
与中性起点PredicateBuilder.And
,并PredicateBuilder.False
与PredicateBuilder.Or
。
How it Works The True and False methods do nothing special: they are simply convenient shortcuts for creating an Expression> that initially evaluates to true or false. 工作原理True和False方法没有什么特别之处:它们只是用于创建最初计算为true或false的Expression>的简便快捷方式。 So the following:
所以以下内容:
var predicate = PredicateBuilder.True (); var predicate = PredicateBuilder.True(); is just a shortcut for this:
只是这个的捷径:
Expression> predicate = c => true; Expression> predicate = c => true; When you're building a predicate by repeatedly stacking and/or conditions, it's useful to have a starting point of either true or false (respectively).
当您通过重复堆叠和/或条件构建谓词时,有一个起始点为true或false(分别)是有用的。
Info complete http://www.albahari.com/nutshell/predicatebuilder.aspx 信息完整http://www.albahari.com/nutshell/predicatebuilder.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.