Possible Duplicate:
Why is there not a ForEach extension method on the IEnumerable interface?可能重复:为什么IEnumerable接口上没有ForEach扩展方法?
EDIT
For reference, here's the blog post which eric referrrred to in the comments
作为参考,这里是eric在评论中引用的博客文章
http://blogs.msdn.com/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx
ORIG
More of a curiosity I suppose but one for the C# Specification Savants...
我想更多的好奇心,但C#规范Savants的一个...
Why is it that the ForEach() clause doesn't work (or isn't available) for use on IQueryable/IEnumerable result sets...
为什么ForEach()子句在IQueryable / IEnumerable结果集上不起作用(或不可用)...
You have to first convert your results ToList() or ToArray() Presumably theres a technical limitation to the way C# iterates IEnumerables Vs. Lists... Is it something to do with the Deferred Execution's of IEnumerables/IQuerable Collections. e.g.
你必须首先转换你的结果ToList()或ToArray()大概是对C#迭代IEnumerables Vs的方式的技术限制。列表......是否与IEnumerables / IQuerable Collections的延迟执行有关。例如
var userAgentStrings = uasdc.UserAgentStrings
.Where<UserAgentString>(p => p.DeviceID == 0 &&
!p.UserAgentString1.Contains("msie"));
//WORKS
userAgentStrings.ToList().ForEach(uas => ProcessUserAgentString(uas));
//WORKS
Array.ForEach(userAgentStrings.ToArray(), uas => ProcessUserAgentString(uas));
//Doesn't WORK
userAgentStrings.ForEach(uas => ProcessUserAgentString(uas));
2 个解决方案
#1
What an amazing coincidence, I just now wrote a blog article about this very question. It
will be
was published May 18th. There is no technical reason why we (or you!) couldn't do this. The reasons why not are philosophical. See my blog next week for my argument.
多么令人惊奇的巧合,我刚才写了一篇关于这个问题的博客文章。它将于5月18日出版。没有技术原因我们(或你!)无法做到这一点。不是哲学的原因。我的论点下周见我的博客。
#2
It's perfectly possible to write a ForEach
extension method for IEnumerable<T>
.
为IEnumerable
I'm not really sure why it isn't included as a built-in extension method:
我不确定为什么它不作为内置扩展方法包含在内:
- Maybe because
ForEach
already existed onList<T>
andArray
prior to LINQ. - Maybe because it's easy enough to use a
foreach
loop to iterate the sequence. - Maybe because it wasn't felt to be functional/LINQy enough.
- Maybe because it isn't chainable. (It's easy enough to make a chainable version that
yield
s each item after performing an action, but that behaviour isn't particularly intuitive.)
也许是因为ForEach已经存在于LINQ之前的List
也许是因为使用foreach循环迭代序列很容易。
也许是因为感觉不够功能/ LINQy。
也许是因为它不可链接。 (很容易制作一个可链接的版本,在执行操作后产生每个项目,但这种行为并不是特别直观。)
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
{
if (source == null) throw new ArgumentNullException("source");
if (action == null) throw new ArgumentNullException("action");
foreach (T item in source)
{
action(item);
}
}
#1
What an amazing coincidence, I just now wrote a blog article about this very question. It
will be
was published May 18th. There is no technical reason why we (or you!) couldn't do this. The reasons why not are philosophical. See my blog next week for my argument.
多么令人惊奇的巧合,我刚才写了一篇关于这个问题的博客文章。它将于5月18日出版。没有技术原因我们(或你!)无法做到这一点。不是哲学的原因。我的论点下周见我的博客。
#2
It's perfectly possible to write a ForEach
extension method for IEnumerable<T>
.
为IEnumerable
I'm not really sure why it isn't included as a built-in extension method:
我不确定为什么它不作为内置扩展方法包含在内:
- Maybe because
ForEach
already existed onList<T>
andArray
prior to LINQ. - Maybe because it's easy enough to use a
foreach
loop to iterate the sequence. - Maybe because it wasn't felt to be functional/LINQy enough.
- Maybe because it isn't chainable. (It's easy enough to make a chainable version that
yield
s each item after performing an action, but that behaviour isn't particularly intuitive.)
也许是因为ForEach已经存在于LINQ之前的List
也许是因为使用foreach循环迭代序列很容易。
也许是因为感觉不够功能/ LINQy。
也许是因为它不可链接。 (很容易制作一个可链接的版本,在执行操作后产生每个项目,但这种行为并不是特别直观。)
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
{
if (source == null) throw new ArgumentNullException("source");
if (action == null) throw new ArgumentNullException("action");
foreach (T item in source)
{
action(item);
}
}