AidaM AidaM - 1 year ago 115
C# Question

C# System.Collections.Generic.Icollection<> does not contain definition for RemoveAll

This is part of code that works perfectly fine on my coworkers machine, but when I try to compile the solution I get the error:
'System.Collections.Generic.ICollection' does not contain a definition for 'RemoveAll' and no extension method 'RemoveAll' accepting a first argument of type 'System.Collections.Generic.ICollection' could be found (are you missing a using directive or an assembly reference?)

I cross checked our references and they seem to match. I have reference to System.Linq and EntityFramework. I tried cleaning and recompiling but this error still sticks.

public void CleanClearinghouse()
x =>
string.IsNullOrWhiteSpace(x.ClearingHouseName) &&
string.IsNullOrWhiteSpace(x.TradingPartnerName) && !x.StartDate.HasValue);

I have a feeling I am missing an assembly reference or something similar.
I would appreciate any tips on where to look for solution, but no suggestions for changing the code.

Answer Source

It's true that ICollection<T> does not contain a method called RemoveAll. The class that does have RemoveAll is List<T>, which is probably the actual, concrete type of your variable.

However, if your property is of type ICollection, the compiler has no way of knowing that it's actually a List.

Say, something like this:

public class MyClass 
    public ICollection<string> ClearinghousePartners {get;set;}
    public MyClass() 
        ClearingHousePartners = new List<string>();

Won't compile, because the List<string> is exposed as an ICollection<string>.

One way of fixing it is to change the property definition to be List<T> instead of ICollection.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download