I have this code
public static class FallbackUtils<TResult, TRequest>
where TRequest : IFallbackRequest
{
public static IEnumerable<TResult> GetAllValues(TRequest request,
Func<TRequest, TResult> callback)
{
yield return callback(request);
while (request.CanFallback())
{
request.Fallback();
yield return callback(request);
}
}
}
Sometimes TResult is going to be an IEnumerable. In those cases I want to be able to keep not only null but empty collections from being returned. The problem is that I don't know how to ask TResult if it's empty I know only how to ask if it's null. Something like this
public static IEnumerable<TResult> GetAllValues(TRequest request,
Func<TRequest, TResult> callback, bool ignoreNullOrEmpty = false)
{
var item = callback(request);
if (ignoreNullOrEmpty)
{
if (item != null && item.Any())
yield return item;
}
else
yield return item;
while (request.CanFallback())
{
request.Fallback();
item = callback(request);
if (ignoreNullOrEmpty)
{
if (item != null && item.Any())
yield return item;
}
else
yield return item;
}
}
I want to do the filtering inside this method to clean up a lot of duplicate code that is doing this same thing.
Any ideas of what should I do or what I'm doing wrong? Thanks in advance.