5

I have created a subclass of a generic list so that I could implement a new interface

public class CustomersCollection : List<Customer>, IEnumerable<SqlDataRecord>
{
...
}

When I change the field definition to the new class (see example of old and new lines below) I get all sorts of compile errors on things that should exist in the original list.

public CustomersCollection Customers { get; set; } 
public void Sample()
{
    Console.WriteLine(Customers.Where(x=>x.condition).First().ToString());
}

Why does CustomersCollection does not inherit the IQueryable, IEnumerable interface implementations for List?

The official error is:

'CustomersCollection' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'CustomersCollection' could be found (are you missing a using directive or an assembly reference?)


It turns out that the custom implementation of IEnumerable causes all the extension methods that apply to IEnumerable to fail. Whats going on here?

1
  • Are you sure it makes any kind of sense at all to inherit from a class that implements IEnumerable<Customer> but override this to implement IEnumerable<SqlDataRecord>? Wouldn't it be better to simply add a method that returns IEnumerable<SqlDataRecord> to avoid complicating things? Commented Jun 24, 2010 at 18:09

1 Answer 1

11

The extension methods are available to a class that inherits from List<T>. Perhaps you need to add using System.Linq; to your code file? Also check that you have a reference to System.Core.dll.

Edit

Since you have List<U> and IEnumerable<T> being inherited/implemented by the same class, you need to provide the type when you use the extension methods. Example:

CustomerCollection customers = new CustomerCollection();
customers.Add(new Customer() { Name = "Adam" });
customers.Add(new Customer() { Name = "Bonita" });
foreach (Customer c in customers.Where<Customer>(c => c.Name == "Adam"))
{
    Console.WriteLine(c.Name);
}

... based on

class Customer { public string Name { get; set; } }    

class Foo { }

class CustomerCollection : List<Customer>, IEnumerable<Foo>
{
    private IList<Foo> foos = new List<Foo>();

    public new IEnumerator<Foo> GetEnumerator()
    {
        return foos.GetEnumerator();
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Where is not inherited, but it requires implementation of IEnumerable, which here is provided through inheritance. Agreed that CustomersCollection has everything needed to be used in LINQ.
Thanks for the reply, but that is not the solution. This code was working with List<Customer>. As soon as I change it to CustomerCollection, the code fails to compile. There are no missing references or namespace imports. Thats why I'm confused.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.