0

I have a function that returns Func<T,TResult>. I want to return multiple funcs in a single func with || of one another.

Below func is just sample but my actual code contains multiple lines of code snippet in func. In which I have to check for each value of comma separated string. Like below function there are multiple functions which returns Func<string, bool>. And all these func will combine as && for further use

public static Func<string, bool> GetData(string request)
{
    var data = request.Split(',');
    //This func is just sample but my actual code contains multiple lines of code snippet in func. In which I have to use each value of comma separated string.
    Func<string, bool> selector = str => str.ToUpper() == data[0] || str => str.ToLower() == data[0] ;
    
    for(int i = i; i < data.Length; i++)
    {
        Func<string, bool> selector1 = str => str.ToUpper() == data[i]  || str => str.ToLower() == data[i] ;
        
        // something like this func || func1 || func2 || func3... and so on
        selector = selector || selector1;
    }
    
    return selector;
}
Anubhit
  • 11
  • 2
  • By `||` do you mean you want to include both functions, or do you mean you want to include one function *or* the other? – Jacob Lockard Sep 30 '21 at 18:43
  • Does this answer your question? [How do I dynamically create an Expression> predicate from Expression>?](https://stackoverflow.com/questions/5094489/how-do-i-dynamically-create-an-expressionfuncmyclass-bool-predicate-from-ex) – GSerg Sep 30 '21 at 18:46
  • Do you need just execute such functions to first result true, or you need to combine Expressions for Linq query? – Leszek Mazur Sep 30 '21 at 18:47
  • perhaps replace `selector || selector 1` with `s => selector(s) || selector1(s)`. – Roy Cohen Sep 30 '21 at 18:53
  • @Anubnit Please provide a full example where you might need `GetData`. Is it Entity Framework? – Andriy Shevchenko Sep 30 '21 at 18:57
  • @JacobLockard My actual func is type of query. So all the func are multiple queries. So I want a combine func which contains all the queries as ||. – Anubhit Sep 30 '21 at 19:21
  • @RoyCohen I have to append the func in a loop. – Anubhit Sep 30 '21 at 19:24
  • @LeszekMazur I need combine expression. As there are multiple functions like this function and all return Func. I have to combine all this func expression using AND operator(&&) for further use – Anubhit Sep 30 '21 at 19:35

3 Answers3

1

I don't think you need multiple Funcs. Just use LINQ's Any function

public static Func<string, bool> GetData(string request)
{
    var data = request.Split(',');
    Func<string, bool> selector = str =>
        data.Any(d => d.Equals(str, StringComparison.OrdinalIgnoreCase));
    
    return selector;
}

Alternatively you can use Contains

    Func<string, bool> selector = str =>
        data.Contains(str, StringComparer.OrdinalIgnoreCase);

Note: OrdinalIgnoreCase is more efficient than calling ToUpper each time

Charlieface
  • 52,284
  • 6
  • 19
  • 43
0

This problem you can solve easier:

public static Func<string, bool> GetData(string request)
{
  return str => request.Split(',').contains(str.ToUpper());
}
Leszek Mazur
  • 2,443
  • 1
  • 14
  • 28
0

Assuming you do need to use a list of Funcs, you can still combine them using LINQ

public static Func<string, bool> GetData(string request)
{
    var data = request.Split(',');
    var selectors = data.Select(d =>
        new Func<string, bool>(str => str.ToUpper() == d || str.ToLower() == d))  // would never use such code, but just your example
        .ToList();  // this is your list of selectors
    
    return str => selectors.Any(sel => sel(str));
}
Charlieface
  • 52,284
  • 6
  • 19
  • 43