结合使用AND和OR的C#谓词构建器 [英] C# Predicate builder with using AND with OR

查看:92
本文介绍了结合使用AND和OR的C#谓词构建器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程:

public class testClass
{
    public string name { get; set; }
    public int id { get; set; }
    public int age { get; set; }
}

和以下代码:

            var list = new List<testClass>();
            list.Add(new testClass { name = "name", id = 1, age = 30 });
            list.Add(new testClass { name = "name", id = 2, age = 22 });
            list.Add(new testClass { name = "name", id = 3, age = 20 });
            list.Add(new testClass { name = "name", id = 4, age = 30 });
            list.Add(new testClass { name = "name", id = 5, age = 27 });
            list.Add(new testClass { name = "name", id = 6, age = 30 });

            var qble = list.AsQueryable();

            var pred = PredicateBuilder.New<testClass>();
            pred.Or(x => x.name == "name" && x.id == 1);
            pred.Or(x => x.age == 30);
            var predQuery = qble.AsExpandable().Where(pred);

我的目的是创建一个查询,该查询返回所有记录,其中:

My aim is to create a query that returns all records where:

id = 1 and name = "name"

OR

age = 30

因此对于上面的查询,它应返回索引为0、1、5的项目

So for the query above, it should return the items at index 0, 1, 5

对于上述查询,它确实可以实现.

For the above query it does as I want.

但是,我现在想通过组合一组查询而不是显式定义它们来构建谓词.因此,我现在有以下两个查询:

However, I now want to the build the predicate by combining a set of queries, rather than explicitly defining them. So I now have the following 2 queries:

var query1 = list.Where(x => x.name == "name" && x.id == 1);
var query2 = list.Where(x => x.age == 30);

,并且我想基于变量query1query2来构建查询,而无需显式定义条件-因为这些条件将被动态定义并且我不知道它们是什么,并且它们将在不同的地方.

and I want to build the query based on the variables query1 and query2, without explicitly defining the conditions - as these conditions will be dynamically defined and I do not know what they are,and they will be defined in different places.

我的猜测是我需要做这样的事情(从上面继续):

My guess is I need to do something like this (continuing from above):

var qble = list.AsQueryable();

var query1 = list.Where(x => x.name == "name" && x.id == 1);
var query2 = list.Where(x => x.age == 30);

var pred = PredicateBuilder.New<testClass>();
pred.Or(query1);
pred.Or(query2);
var predQuery = qble.AsExpandable().Where(pred);

但这不是很正确,因为谓词生成器不会接受该查询作为参数.

but this is not quite correct as the predicate builder will not accept the query as a parameter.

可以做到吗?

推荐答案

您可以创建两个Predicate<T>,并在最后的.Where调用中调用它们.

You could create two Predicate<T> and invoke them in your .Where call at the end.

var qble = list.AsQueryable();

var query1 = new Predicate<testClass>(x => x.name == "name" && x.id == 1);
var query2 = new Predicate<testClass>(x => x.age == 30);

var predQuery = qble.AsExpandable().Where(x => query1(x) || query2(x));

或者您可以预先构建另一个Predicate<T>并使用它

Or you could build another Predicate<T> beforehand and use this

var query = new Predicate<testClass>(x => query1(x) || query2(x));
var predQuery = qble.AsExpandable().Where(query);

这篇关于结合使用AND和OR的C#谓词构建器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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