本文介绍了LinqKit PredicateBuilder返回所有和非行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用的的创建谓词与OR条件,是不可能的LINQ表达式。

I'm beginning to use LinqKit's PredicateBuilder to create predicate's with OR conditions which is not possible with Linq expressions.

我现在面临的问题是,如果我用 PredicateBuilder.True<开始; myEntity所>()它的返回所有行如果我开始使用 PredicateBuilder.False<&myEntity所GT;()它的从我用什么表情返回非行,除了!看看下面的代码:

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;
        }



这应该返回有IsActive ==真行,但它返回的所有!行

It should return the rows which has IsActive == true, but it returns all rows!

我曾尝试 PredicateBuilder.True 的所有可能的组合| PredicateBuilder.False | 方法,其中非工程!

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 ANDed 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.

尝试:

    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返回所有和非行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 16:47