这是一些代码的一部分,在我同事的机器上工作得很好,但是当我试图编译解决方案时,我得到了错误:
我交叉核对了我们的参考资料,它们似乎是一致的。我有一个参考系统。Linq和EntityFramework。我尝试了清理和重新编译,但这个错误仍然存在。System.Collections.Generic。ICollection'没有包含'RemoveAll'的定义,也没有扩展方法'RemoveAll'接受类型为'System.Collections.Generic '的第一个参数。可以找到ICollection(您是否缺少using指令或程序集引用?)
public void CleanClearinghouse()
{
this.ClearinghousePartners.RemoveAll(
x =>
string.IsNullOrWhiteSpace(x.ClearingHouseName) &&
string.IsNullOrWhiteSpace(x.TradingPartnerName) && !x.StartDate.HasValue);
}
我有一种感觉,我错过了一个汇编参考或类似的东西。我将感谢任何关于在哪里寻找解决方案的提示,但没有更改代码的建议。
确实,ICollection<T>
不包含一个名为RemoveAll的方法。具有RemoveAll的类是List<T>
,这可能是变量的实际具体类型。
但是,如果你的属性是ICollection类型,编译器无法知道它实际上是一个List。
比如说,像这样:
public class MyClass
{
public ICollection<string> ClearinghousePartners {get;set;}
public MyClass()
{
ClearingHousePartners = new List<string>();
}
}
不会编译,因为List<string>
被暴露为ICollection<string>
。
修复它的一种方法是将属性定义更改为List<T>
而不是ICollection
由于ICollection
没有RemoveAll
,如果您想保留已有的代码,一种选择是自己实现RemoveAll
,如下所示:
public static void RemoveAll<T>(this ICollection<T> collection, Predicate<T> match)
where T : ClearinghousePartners
{
if (match == null)
throw new ArgumentNullException("match");
collection.Where(entity =>
match(entity)).ToList().ForEach(entity => collection.Remove(entity));
}
注意:这是一个扩展方法,所以需要放在一个静态类中。