C# 如何有效地将带有AND/OR运算符的布尔值列表缩减为单个结果

C# 如何有效地将带有AND/OR运算符的布尔值列表缩减为单个结果,c#,boolean,boolean-logic,C#,Boolean,Boolean Logic,我正在寻找一种有效的方法来减少包含和/或C#条件的真/假语句列表 考虑通过一个简单的等式(例如: if (item[1].operator == "AND") // Evaluate NEXT item's operator (AND/OR) { result == item[0] && item[1] // Which would resolve out to T } else if (item[1].operator == "OR") { } *但在比较项目

我正在寻找一种有效的方法来减少包含和/或C#条件的真/假语句列表

考虑通过一个简单的等式(例如:

if (item[1].operator == "AND") // Evaluate NEXT item's operator (AND/OR)
{  
   result == item[0] && item[1]  // Which would resolve out to T 
}
else if (item[1].operator == "OR")
{

}
*但在比较项目列表时,我必须包括逻辑,也包括和/或操作

我需要将其简化为单个结果T/F结果的数据示例。 我有一个布尔值和运算符(和/或)的列表,需要找到一种方法

public class TestProcessor() {
    bool Result { get; set; }
    string Operator { get; set; }  // Specifies if it is tied together in the statement with an AND or an OR 
...
}


public class TestLogic() {
    List<TestProcessor> TestProcesses { get; set; }

    public bool ReduceAll() {
     ...  This would essentially take all items in the 'TestProcessor' property and resolve out the list of items accordingly in the order they are in from the list and in conjunction to if they are linked together with an AND or an OR
    }
}


If I had a list of Processes that Result in 
* Note:  (First item and last item don't need AND/OR operators)
[0]  Result = True,  Operator = null    
[1]  Result = True,  Operator = OR   
[2]  Result = True,  Operator = OR   
[3]  Result = True,  Operator = AND   
[4]  Result = False,  Operator = OR
[5]  Result = True,  Operator = AND      

(T) OR (T) OR (T) AND (T) OR (F) AND (T)  which would resolve out to: True 

Is there an easy way or a more dynamic way to do this?  Or is there a way to more easily just pass the data to the compiler to evaluate out the entire list of items for it to evaluate without all the extra effort?
公共类TestProcessor(){
bool结果{get;set;}
字符串运算符{get;set;}//指定它在语句中是否与AND或or绑定在一起
...
}
公共类TestLogic(){
列出testprocesss{get;set;}
公共图书馆{
…这将从本质上获取“TestProcessor”属性中的所有项,并根据它们在列表中的顺序以及它们是否与and或or链接在一起,相应地解析出项列表
}
}
如果我有一个导致
*注:(第一项和最后一项不需要和/或运算符)
[0]结果=True,运算符=null
[1] 结果=真,运算符=或
[2] 结果=真,运算符=或
[3] 结果=真,运算符=和
[4] 结果=假,运算符=或
[5] 结果=真,运算符=和
(T) 或(T)或(T)和(T)或(F)和(T),其解析结果为:真
有没有一种简单的方法或者更具动态性的方法来实现这一点?或者有没有一种方法可以更容易地将数据传递给编译器,以计算出整个项目列表,以便编译器无需付出额外的努力就可以进行计算?

您所说的简单迭代可能是最好的选择……但是如果您真的喜欢一些LINQ-
可枚举的方法。聚合是您正在寻找的方法。大致如下:

var result = list.Aggregate(true, (sum, cur) => 
      cur.Operator == "AND" ? sum && cur.Result : sum || cur.Result);

确保你考虑了操作的顺序:[ Alexei Levenkov ]很棒的推荐!非常感谢你的快速响应,对我现在使用它有很大的帮助。非常感谢。