を使用する場合は、 DataSet、DataTable、およびDataViewのみをソースとして使用できるようですBindingSource.Find
。IQueryableとIEnumerableをバインディングソースにバインドしていますが、BindingSource.Find
時間が重要であるため、コードの連なりを自分で作成せずに、メソッドの「便利さ」を楽しみたいと思っています。
誰かが既存の実装、またはこれを達成するのに役立つ少なくとも詳細な「ハウツー」記事を知っていますか?
を使用する場合は、 DataSet、DataTable、およびDataViewのみをソースとして使用できるようですBindingSource.Find
。IQueryableとIEnumerableをバインディングソースにバインドしていますが、BindingSource.Find
時間が重要であるため、コードの連なりを自分で作成せずに、メソッドの「便利さ」を楽しみたいと思っています。
誰かが既存の実装、またはこれを達成するのに役立つ少なくとも詳細な「ハウツー」記事を知っていますか?
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));
}