0

を使用する場合は、 DataSetDataTable、およびDataViewのみをソースとして使用できるようですBindingSource.FindIQueryableIEnumerableをバインディングソースにバインドしていますが、BindingSource.Find時間が重要であるため、コードの連なりを自分で作成せずに、メソッドの「便利さ」を楽しみたいと思っています。

誰かが既存の実装、またはこれを達成するのに役立つ少なくとも詳細な「ハウツー」記事を知っていますか?

4

1 に答える 1

0

Type.GetPropertyメソッドとArray.FindIndexメソッドを使用して、簡潔な拡張メソッドを作成できます。

public static int Find<T>(this IEnumerable<T> items, string propertyName, Object key)
{
    PropertyInfo property = typeof(T).GetProperty(propertyName);
    if(property == null)
    {
        throw new ArgumentException(String.Format("Type {0} contains no property named \"{1}\". ",
                                    typeof(T).Name, propertyName), "propertyName");
    }
    return Array.FindIndex(items.ToArray(), i => Object.Equals(property.GetValue(i, null), key));       
}
于 2012-09-27T13:29:10.240 に答える