如何在CollectionBase上执行LINQ操作

本文关键字:执行 LINQ 操作 CollectionBase | 更新日期: 2023-09-27 18:06:34

我正在编写一个c#表单应用程序,我有一个PropertyGrid控件的以下集合:

public class CustomWebpageJavaScriptFunctionCollection : CollectionBase, ICustomTypeDescriptor
{
    public void Add(CustomWebpageJavaScriptFunction obj)
    {
        this.List.Add(obj);
    }
    public void Remove(CustomWebpageJavaScriptFunction obj)
    {
        this.List.Remove(obj);
    }
    public CustomWebpageJavaScriptFunction this[int index]
    {
        get
        {
            return (CustomWebpageJavaScriptFunction)this.List[index];
        }
    }
    public String GetClassName()
    {
        return TypeDescriptor.GetClassName(this, true);
    }
    public AttributeCollection GetAttributes()
    {
        return TypeDescriptor.GetAttributes(this, true);
    }
    public String GetComponentName()
    {
        return TypeDescriptor.GetComponentName(this, true);
    }
    public TypeConverter GetConverter()
    {
        return TypeDescriptor.GetConverter(this, true);
    }
    public EventDescriptor GetDefaultEvent()
    {
        return TypeDescriptor.GetDefaultEvent(this, true);
    }
    public PropertyDescriptor GetDefaultProperty()
    {
        return TypeDescriptor.GetDefaultProperty(this, true);
    }
    public object GetEditor(Type editorBaseType)
    {
        return TypeDescriptor.GetEditor(this, editorBaseType, true);
    }
    public EventDescriptorCollection GetEvents(Attribute[] attributes)
    {
        return TypeDescriptor.GetEvents(this, attributes, true);
    }
    public EventDescriptorCollection GetEvents()
    {
        return TypeDescriptor.GetEvents(this, true);
    }
    public object GetPropertyOwner(PropertyDescriptor pd)
    {
        return this;
    }

    /// <summary>
    /// Called to get the properties of this type. Returns properties with certain
    /// attributes. this restriction is not implemented here.
    /// </summary>
    /// <param name="attributes"></param>
    /// <returns></returns>
    public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
    {
        return GetProperties();
    }
    /// <summary>
    /// Called to get the properties of this type.
    /// </summary>
    /// <returns></returns>
    public PropertyDescriptorCollection GetProperties()
    {
        // Create a collection object to hold property descriptors
        PropertyDescriptorCollection pds = new PropertyDescriptorCollection(null);
        // Iterate the list of employees
        for (int i = 0; i < this.List.Count; i++)
        {
            // Create a property descriptor for the employee item and add to the property descriptor collection
            CustomWebpageJavaScriptFunctionCollectionPropertyDescriptor pd = new CustomWebpageJavaScriptFunctionCollectionPropertyDescriptor(this, i);
            pds.Add(pd);
        }
        // return the property descriptor collection
        return pds;
    }
}

如何在上述集合上执行LINQ查询的最佳方式?

例如,如何对CollectionBase List的内容执行。where()操作?

如何在CollectionBase上执行LINQ操作

LINQ需要泛型枚举,CollectionBase是非泛型枚举。先执行OfType<object>(),然后可以在IEnumerable<object>实例上使用LINQ。

编辑:正如评论中所指出的,您也可以使用Cast<object>()。这两种方法的区别在于,OfType将包括可以在枚举中强制转换为指定类型的元素,并跳过其余的元素,而Cast要求所有元素都可以强制转换为该类型(否则会抛出异常)。Cast更快,因为它跳过检查,直接进行强制转换。

Cast迭代器来源:

foreach (object obj in source) yield return (TResult)obj;

OfType迭代器来源:

foreach (object obj in source) {
    if (obj is TResult) yield return (TResult)obj;
}