0

I'd like convert string to ConditionalOperator, few sample :

  • "and" to &&
  • "or" to ||
  • "greatherthan" to >=
  • "contains" to .Contains

Something like this :

public static class Extension
{
    public static Operator ConditionalOperator(this string logic)
    {
        switch (logic)
        {
            case "and": return   // return && conditionaloperator
            case "or": return    // return || conditionaloperator
            case "greatherthan": return // return >= conditionaloperator
        }
    }
}

The second argument is not all the time a string but can be int, double, bool, ...

I'd like build something like : ConditionalOperator("==", "StringToSearch") return a Func<>

Do you have an idea how to do this ?

Thanks,

Update 1, to clarify: I received a list of object "Filter"enter image description here

The "FilterOperator" all possible values for a CondtionalOperator.

5
  • 1
    What is Operator or Opertor? What is the second argument? Commented Sep 10, 2023 at 8:13
  • I think this post might be of help to you. Commented Sep 10, 2023 at 8:14
  • @StephenRoss I read this post but I don't understand where "Operator" comes from Operator.LessThan<T>; Commented Sep 10, 2023 at 8:19
  • @shingo it's a typo Commented Sep 10, 2023 at 8:22
  • 1
    How would you do Contains with an int? Or a bool? Commented Sep 10, 2023 at 9:12

1 Answer 1

1

You can use generics

public static bool Operator(string logic, T x, T y) where T : IComparable<T>
{
     var compare = Comparer<T>.Default.Compare(x, y);
     return logic switch
     {
         ">" => compare > 0,
         "<" => compare < 0,
         "==" => compare == 0,
         _ => throw new Exception("invalid logic"),
     };
 }

Optionally you could pass an explicit IComparer

public static bool Operator(string logic, T x, T y, IComparer<T> comparer)
{
     var compare = comparer.Compare(x, y);
     return logic switch
     {
         ">" => compare > 0,
         "<" => compare < 0,
         "==" => compare == 0,
         _ => throw new Exception("invalid logic"),
     };
 }

To return a Func<T, T, bool> you can just do

public static Func<T, T, bool> Operator(string logic, T x, T y, IComparer<T> comparer)
{
     return logic switch
     {
         ">" => (x, y) => comparer.Compare(x, y) > 0,
         "<" => (x, y) => comparer.Compare(x, y) < 0,
         "==" => (x, y) => comparer.Compare(x, y) == 0,
         _ => throw new Exception("invalid logic"),
     };
 }

For string.Contains you should probably make a separate overload just for string, as that cannot be genericized.

Sign up to request clarification or add additional context in comments.

3 Comments

if there is Contains for a string ?
I don't understand your question. Yes there is a Contains function, but that's not what you asked, and it wouldn't make sense to use that with generics. Perhaps you should make a separate overload just for string.
I updated the question to clarify. It's more create an expression.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.