I have a problem trying to cast a Predicate<T>
to Func<T, bool>
. I have a method that accepts a object[]
variable as argument:
protected abstract Predicate<TResults> PredicateFind(object[] values);
It works perfectly for List.Find
:
protected TResults Find(params object[] values) => Resultados.Find(PredicateFind(values));
but trying to use it with List.Any
:
protected TResults Any(params object[] values) => Resultados.Any(r => new Func<TResults, bool>(x => (bool)PredicateFind(values) == true));
gives:
CS0030: Cannot convert type 'System.Predicate<TResults>' to 'bool'
Trying to change it to
protected TResults Any(params object[] values) => Resultados.Any(PredicateFind(values));
gives:
CS1503: Argument 2: cannot convert from 'System.Predicate<TResults>' to 'System.Function<TResults, bool>'
And changing it to
protected TResults Any(params object[] values) => Resultados.Any(new Func<TResults, bool>(PredicateFind(values)));
gives
CS0029: Cannot implicitly convert type 'bool' to 'TResults'
How should I call Predicate(object[] values)
to be able to use it with Any
?
Thank you
EDIT
Finally the problem was in
protected TResults Any(params object[] values) => Resultados.Any(PredicateFind(values));
First I had to change the return type to bool
protected bool Any(params object[] values) => Resultados.Any(PredicateFind(values));
and second I had to change the argument to r => PredicateFind(values).Invoke(r)
protected bool Any(params object[] values) => Resultados.Any(r => PredicateFind(values).Invoke(r));
Now it's fixed.
Thank you all.