I’d like to make this function more generic by specifying the property of Foo that I search on in the function arguments. At the moment I have to have a function for every property of Foo rather than just one generic function.
private Func<Foo, bool> ByName(bool _exclude, string[] _searchTerms)
{
if (_exclude)
{
return x => !_searchTerms.Contains( x.Name.Replace(" ", "").ToLower() );
}
return x => _searchTerms.Contains( x.Name.Replace(" ", "").ToLower() );
}
Is it possible to make this function more generic to be able to pass the search property of Foo?
You can easily add a
Func<Foo, string>:You would call it like that:
Please note that this method is not generic. It only supports properties of type
string, because your search method usesReplaceon the property and yoursearchTermsare alsostrings.BTW: Please note the way I named the parameters. The .NET naming convention doesn’t use underscores for parameters.