问题描述
我开始使用 使用OR条件创建谓词,这是不可能的Linq表达式。
我遇到的问题是如果我以 PredicateBuilder.True< MyEntity>()
它返回所有行,如果我以 PredicateBuilder.False< MyEntity>()
开头,返回非行从我使用什么表达式!看看下面的代码:
var pre = PredicateBuilder.True< MyEntity>();
pre.And(m => m.IsActive == true);
使用(var db = new TestEntities())
{
var list = db.MyEntity.AsExpandable()。其中(pre).ToList();
dataGridView1.DataSource = list;
}
它应该返回IsActive == true的行,但返回所有行$!
我已尝试过所有可能的组合 PredicateBuilder.True
PredicateBuilder.False
with And
或
方法,而不是工作!
和
extension-method不会修改原始谓词 - 它返回一个表示原始谓词的新的 AND与指定的谓词一起使用AND
ed。
有效地,您的操作不会更改您的 pre
变量引用的谓词,这意味着您最终会基于您是否将原始谓词初始化为 true
或 false
。的所有记录或无记录。
尝试:
var pre = PredicateBuilder.True< MyEntity>();
pre = pre.And(m => m.IsActive);
如果您计划或
一起谓词请记住先开始一个 false
初始谓词。
var pre = PredicateBuilder.False< MyEntity>();
pre = pre.Or(m => m.IsActive);
I'm beginning to use LinqKit's PredicateBuilder to create predicate's with OR conditions which is not possible with Linq expressions.
The problem I'm facing is if I begin with PredicateBuilder.True<MyEntity>()
it returns all rows and if I begin with PredicateBuilder.False<MyEntity>()
it returns non rows, apart from what expressions I use! look at the code below:
var pre = PredicateBuilder.True<MyEntity>();
pre.And(m => m.IsActive == true);
using (var db = new TestEntities())
{
var list = db.MyEntity.AsExpandable().Where(pre).ToList();
dataGridView1.DataSource = list;
}
It should return the rows which has IsActive == true, but it returns all rows!
I have tried all the possible combinations of PredicateBuilder.True
| PredicateBuilder.False
with And
| Or
methods, non of them works!
The And
extension-method does not modify the original predicate - it returns a new predicate representing the original predicate AND
ed together with the specified predicate.
Effectively, your operations are not changing the predicate referred to by your pre
variable, meaning you end up with either all or none of the records based on whether you initialized the original predicate to true
or false
.
Try:
var pre = PredicateBuilder.True<MyEntity>();
pre = pre.And(m => m.IsActive);
If you are planning toOR
predicates together, remember to start off with a false
initial predicate.
var pre = PredicateBuilder.False<MyEntity>();
pre = pre.Or(m => m.IsActive);
这篇关于LinqKit PredicateBuilder返回所有行或非行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!