在我刚刚完成的LinqKit更新之后,我的Visual Studio2015开始在编译时抛出一个异常:
找不到方法:'Void LinqKit.Extensions.ForEach(System.Collections.Generic.IEnumerable1<!!0>, System.Action1)'.
我已经将我的LinqKit从1.1.4.1版本更新到1.1.7.1版本。
我在代码中使用了许多ForEach语句,但幸运的是,抛出的异常并没有说出导致错误的确切行。异常弹出窗口仅指出包含这些ForEach语句的方法调用。
提前谢谢。
编辑
我以这样的方式进行了ForEach调用:
someCollection.ForEach(s => {
// something here
});
dictionaryA.ForEach(d => {
// something here
});
dictionaryB.Keys.ForEach(d => {
// something here
})发布于 2016-10-03 21:53:00
此方法与LinqKit无关,已被删除。
默认情况下,.NET有针对List<T>的.ToList(),所以只需使用someCollection.ToList().ForEach(...)
ToList没有在.NET中为IEnumerable实现的原因是,IEnumerable应该被认为是不可变的集合,而ForEach是一个会产生一些副作用的可变方法。
发布于 2016-09-08 06:36:41
Chee's Burgers提供的link表明,当ForEach存在时,它只是一个扩展方法,将您的"something here“应用于集合中的每个元素,如下所示:
/// <summary> Default side-effect style enumeration </summary>
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
{
foreach (var element in source)
action(element);
}因此,您可以自己添加扩展方法,也可以按如下方式更新代码
foreach(var s in someCollection)
{
// something here
}
foreach(var d in dictionaryA)
{
// something here
}
foreach(var d in dictionaryB.Keys)
{
// something here
}https://stackoverflow.com/questions/38645955
复制相似问题