Search code examples
c#conditional-statementslogicrules

Is there a way to create a bool function that takes two List objects and returns true or false if a condition is met?


As the title states I am looking for ways to create and be able to interact with bool conditions.

I have tried predicates but they don't work. public static Predicate<(decimal?,decimal?)> Create(Predicate<(decimal?,decimal?)> predicate) => predicate;

this only works as a filter for when I do Condition.Create(c => c.IsSomeCalc(list1, list2))

then the use case is var res=Condition(SomeValList)

there are many overloads for the creation of a condition and not just 1.

what I am trying to do is create a condition that returns true or false if the condition is met

i.e Condition IsLower= Condition.Create(c=>c.IsLower(decimal,decimal)

Then to use

IsLower(5,10)//=true

edit: Say I have a ConditionClass that has the field ConditionOperator and ConditionOperator Is an interface that Implements Classes Like IsGreaterThan and in IsGreaterThan I have the field IsTrue which is boolean and in that class I can have multiple overloads for updating IsTrue

i.e public IsGreaterThan(decimal itm, decimal itm2) { isTrue = itm > itm2; }

and then use case would be another class that use the condition like

Test Test= new Test();
Test.Condition.ConditionOperator= new IsGreaterThanCondition(1,2);
Console.WriteLine($"{Test.Condition.ConditionOperator.IsTrue}";

This is all redundant and less versatile than the extension methods for what I am trying to do.


Solution

  • The Check function.

    public static void Main()
    {
        List<double> listA = new List<double>() { 1, 2, 3 };
        List<double> listB = new List<double>() { 2, 3, 4 };
        
        // Simple example, checking if the sum of the numbers in list A
        // is greater than the sum of the numbers in list B.
        bool result = Check(listA, listB, (a, b) => a.Sum() > b.Sum());
        Console.WriteLine($"Test result: {result}");
    }
        
    public static bool Check<T>(List<T> listA, List<T> listB, Func<List<T>, List<T>, bool> condition)
    {
        return condition(listA, listB);
    }