tags:

views:

74

answers:

1

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?

+9  A: 

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();
    }
}
Anthony Pegram
+1 .... `Where` is not "inherited" from anything, it's an extension method.
Warren
`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.
Ben Voigt
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.
Matt Murrell
@Matt, I've updated my answer.
Anthony Pegram
Good answer. Thanks for the update.
Matt Murrell