如何有效地将带有 AND / OR 运算符的布尔值列表减少为单个结果
How to efficiently reduce list of booleans with AND / OR operators to single result
我正在寻找一种有效的方法来使用 C# 减少包含 AND / OR 条件的 True / False 语句列表。
考虑通过一个简单的等式(例如:
遍历每个并简单地将每个项目评估为列表中的下一个协调项目
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")
{
}
*但我必须包含在比较项目列表时也将包含 AND/OR 操作的逻辑...
我需要缩减为单个结果 T/F 结果的数据示例。
我有一个布尔值和运算符 (AND / OR) 的列表,需要找到
的方法
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?
您所说的简单迭代可能是最好的选择...但是如果您真的喜欢某些 LINQ - Enumerable.Aggregate
就是您正在寻找的方法。大致像:
var result = list.Aggregate(true, (sum, cur) =>
cur.Operator == "AND" ? sum && cur.Result : sum || cur.Result);
我正在寻找一种有效的方法来使用 C# 减少包含 AND / OR 条件的 True / False 语句列表。
考虑通过一个简单的等式(例如:
遍历每个并简单地将每个项目评估为列表中的下一个协调项目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")
{
}
*但我必须包含在比较项目列表时也将包含 AND/OR 操作的逻辑...
我需要缩减为单个结果 T/F 结果的数据示例。 我有一个布尔值和运算符 (AND / OR) 的列表,需要找到
的方法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?
您所说的简单迭代可能是最好的选择...但是如果您真的喜欢某些 LINQ - Enumerable.Aggregate
就是您正在寻找的方法。大致像:
var result = list.Aggregate(true, (sum, cur) =>
cur.Operator == "AND" ? sum && cur.Result : sum || cur.Result);