是否有任何已知的IBIndingList实现支持Find方法,适用于LINQ?

时间:2012-09-26 14:36:51

标签: linq data-binding bindingsource

当您想要使用BindingSource.Find时,似乎只能将 DataSet DataTable DataView 用作来源。我将 IQueryable IEnumerable 绑定到我的绑定源,但非常希望享受BindingSource.Find方法的“便利性”,而无需编写大量的自己编码,因为时间至关重要。

有没有人知道现有的实施方案,或至少详细的“如何”文章可以帮助我实现这一目标?

1 个答案:

答案 0 :(得分:0)

您可以使用Type.GetPropertyArray.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));       
}