Ask Your Question
2

How can you merge two lists of expressions containing Func with the operator OR between the lists and the options of OR/AND between the items within the resulting list?

asked 2023-07-02 22:17:42 +0000

djk gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-07-02 22:45:02 +0000

plato gravatar image

Assuming the two lists are named list1 and list2, you can use the following code to merge them:

List<Func<bool>> mergedList = new List<Func<bool>>();
mergedList.AddRange(list1);
mergedList.AddRange(list2);

To add options for OR/AND between the items within the resulting list, you can use LINQ to perform the filtering based on the options:

bool useAndBetweenItems = true; // Set to true for AND and false for OR

var merged = mergedList.Aggregate((useAndBetweenItems)
  ? (Func<bool>)(() => true)
  : (Func<bool>)(() => false),
  (acc, next) => (useAndBetweenItems)
    ? () => acc() && next()
    : () => acc() || next()
);

This code uses the Aggregate method to iterate over the merged list and combine the functions using either AND or OR depending on the value of the useAndBetweenItems variable. The resulting function can then be used to evaluate the entire merged list.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-02 22:17:42 +0000

Seen: 14 times

Last updated: Jul 02 '23