2014-09-01 80 views
-1

好吧,所以我试图将ForEach扩展方法添加到linq,因为没有forEach for IEnumerables但由于某种原因,我看不到它。无法将扩展方法添加到LINQ

扩展方法是:

public static IEnumerable<TSource> ForEach<TSource>(Func<TSource> action) 
{ 
    yield return action(); 
} 

,当我尝试调用它(gdMainGrid):

+2

这不是一个扩展方法。 – Dirk 2014-09-01 10:54:54

+2

首先,你不觉得一个* extension *方法需要一个'this'第一个参数吗?为什么不在MSDN上查找任何扩展方法来查看它们的签名是什么?最后...... Enumerable.ForEach。叹。 – Jon 2014-09-01 10:55:09

+0

是的,我忘了这一点,但即使添加了这个,它不起作用.. http://i.imgur.com/M94OpQJ.png – 2014-09-01 11:00:58

回答

1

要具有智能感知显示的东西,你在你的代码尝试,你必须添加一个参数。 this IEnumerable<TSource> source

或者它不会是一个IEnumerable<TSource>一个扩展方法(和gdMain.Children.Cast<UIElement>()会返回一个IEnumerable<UIElement>

public static IEnumerable<TSource> ForEach<TSource>(this IEnumerable<TSource> source, Func<TSource> action) 
{ 
    yield return action(); 
}