Extension method on custom collection throwing error

278 views Asked by At

I'm creating a custom collection like this.

 public class ClientBusinessEntityCollection<T> : ICollection<T> where T : EntityBase
{
    /// <summary>
    /// The list business objects
    /// </summary>
    private List<T> listBusinessObjects = null;

    /// <summary>
    /// Initializes a new instance of the <see cref="KddiBusinessEntityCollection{T}"/> class.
    /// </summary>
    public ClientBusinessEntityCollection()
    {
        this.listBusinessObjects = new List<T>();
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="KddiBusinessEntityCollection{T}"/> class.
    /// </summary>
    /// <param name="collection">The collection.</param>
    public ClientBusinessEntityCollection(IEnumerable<T> collection)
    {
        this.listBusinessObjects = new List<T>(collection);
    }

    /// <summary>
    /// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1" />.
    /// </summary>
    /// <value>The count.</value>
    /// <returns>The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1" />.</returns>
    public int Count
    {
        get { return this.listBusinessObjects.Count; }
    }

    /// <summary>
    /// Gets the <see cref="`0"/> at the specified index.
    /// </summary>
    /// <param name="index">The index.</param>
    /// <returns>`0.</returns>
    public T this[long index]
    {
        get
        {
            return this.listBusinessObjects[(int)index];
        }
    }

    /// <summary>
    /// Gets a value indicating whether the <see cref="T:System.Collections.Generic.ICollection`1" /> is read-only.
    /// </summary>
    /// <value><c>true</c> if this instance is read only; otherwise, <c>false</c>.</value>
    /// <returns>true if the <see cref="T:System.Collections.Generic.ICollection`1" /> is read-only; otherwise, false.</returns>
    public bool IsReadOnly
    {
        get { return false; }
    }

    /// <summary>
    /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1" />.
    /// </summary>
    /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1" />.</param>
    public void Add(T item)
    {
        this.listBusinessObjects.Add(item);
    }

    /// <summary>
    /// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1" />.
    /// </summary>
    public void Clear()
    {
        this.listBusinessObjects.Clear();
    }

    /// <summary>
    /// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1" /> contains a specific value.
    /// </summary>
    /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1" />.</param>
    /// <returns>true if <paramref name="item" /> is found in the <see cref="T:System.Collections.Generic.ICollection`1" />; otherwise, false.</returns>
    public bool Contains(T item)
    {
        return this.listBusinessObjects.Contains(item);
    }

    /// <summary>
    /// Sorts the collection.
    /// </summary>
    /// <param name="sorter">The sorter.</param>
    public void SortCollection(Func<EntityBase, object> sorter)
    {
        //// TODO : IMPLEMENT SORTING HERE.
    }

    /// <summary>
    /// Copies to.
    /// </summary>
    /// <param name="array">The array.</param>
    /// <param name="arrayIndex">Index of the array.</param>
    public void CopyTo(T[] array, int arrayIndex)
    {
    }

    /// <summary>
    /// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1" />.
    /// </summary>
    /// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1" />.</param>
    /// <returns>true if <paramref name="item" /> was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1" />; otherwise, false. This method also returns false if <paramref name="item" /> is not found in the original <see cref="T:System.Collections.Generic.ICollection`1" />.</returns>
    public bool Remove(T item)
    {
        return this.listBusinessObjects.Remove(item);
    }

    /// <summary>
    /// Returns an enumerator that iterates through the collection.
    /// </summary>
    /// <returns>A <see cref="T:System.Collections.Generic.IEnumerator`1" /> that can be used to iterate through the collection.</returns>
    public IEnumerator<T> GetEnumerator()
    {
        return this.listBusinessObjects.GetEnumerator();
    }

    /// <summary>
    /// Returns an enumerator that iterates through a collection.
    /// </summary>
    /// <returns>An <see cref="T:System.Collections.IEnumerator" /> object that can be used to iterate through the collection.</returns>
    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return this.listBusinessObjects.GetEnumerator();
    }
}

now I have a collection like this

public ClientBusinessEntityCollection<MyClass> Collection {get; set;}

Now the problem is that when I write LINQ statement on "Collection" it throws null reference exception.

Collection.OrderBy(item=>item.Order);

The collection has an underlying List , but unlike List when you hover your mouse over my custom "Collection" it doesn't shown the number of items. How should I make extension method pick values from the underlying List object when writing LINQ over my custom collection?

Do I need to write custom IEnumerator?

1

There are 1 answers

6
Baldrick On

You need to pass through any enumerator-related calls made to your collection to your underlying list, as follows:

public class MyTest<T> : ICollection<T>
{
    private List<T> myUnderlyingList = new List<T>();

    public IEnumerator<T> GetEnumerator()
    {
        return myUnderlyingList.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

.... etc.

Then LINQ will simply be 'seeing' the underlying List when it performs its operations.

The other thing to make sure is that your underlying List<T> is populated correctly with valid data before you run any LINQ methods on it.