したがって、通常、 にIEnumerable<T>
項目があるかどうかを単純に確認したい場合は、.Any()
代わりに使用.count > 0
します。特に、LINQ-To-Entities のようなものをヒットし.count
ていて、パフォーマンスが大幅に低下する可能性がある場合はそうです。
私が抱えている問題はIValueConverter
、列挙型に項目があるかどうかに応じてオブジェクトの可視性を変更するために を書いていることです:
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();
}
}
IEnumerable<T>
したがって、この状況では、現時点では aを使用できないため、通常の拡張メソッドを an で使用することはできません<T>
。私の現在の実装ICollection
では、常に望ましいとは限らない実装に制限されています。
より効率的な方法でこれを行うにはどうすればよいですか? ベアIEnumerable
(サンズ<T>
) には . がありません.Any()
。