PredicateBuilder< True>之间的差异和PredicateBuilder< False&gt ;? [英] Difference between PredicateBuilder<True> and PredicateBuilder<False>?

查看:417
本文介绍了PredicateBuilder< True>之间的差异和PredicateBuilder< False&gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有代码:

   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 restOfPredicateis true, and false if restOfPredicate is false. Hence why it's considered neutral.

PredicateBuilder.False ,但是等同于 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&lt; True&gt;之间的差异和PredicateBuilder&lt; False&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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