So normally if you want to simply check if an IEnumerable<T>
has any items in you'd use .Any()
instead of .count > 0
- especially when you're hitting things like LINQ-To-Entities and .count
may have a heavy performance penalty.
The problem I have is that I'm writing an IValueConverter
to change an objects visibility depending on whether or not an enumerable has items or not:
public class CollectionEmptyVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
var col = value as ICollection;
if (col == null) { return Visibility.Collapsed.ToString(); }
return (col.Count > 0) ? Visibility.Visible.ToString() : Visibility.Collapsed.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
So in this situation I can't use the normal extension methods on an IEnumerable<T>
because I can't have a <T>
at this time. My current implementation limits me to things that implement ICollection
which may not always be desirable.
How can I do this in a more efficient manner? The bare IEnumerable
(sans <T>
) doesn't have a .Any()
.