C# 使用 Foreach 子句的 Lambda 表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/858978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Lambda Expression using Foreach Clause
提问by Eoin Campbell
Possible Duplicate:
Why is there not a ForEach extension method on the IEnumerable interface?
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
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# 规范学者来说是一种好奇心......
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 的方式的技术限制。列表...是否与 IEnumerables/IQuerable 集合的延迟执行有关。例如
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));
采纳答案by Eric Lippert
What an amazing coincidence, I just now wrote a blog article about this very question. It will bewas 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日。我们(或您!)无法做到这一点没有任何技术原因。原因是哲学上的。我的论点下周见我的博客。
回答by LukeH
It's perfectly possible to write a ForEach
extension method for IEnumerable<T>
.
完全可以ForEach
为IEnumerable<T>
.
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
已经存在的List<T>
和Array
之前LINQ。 - 也许是因为使用
foreach
循环来迭代序列很容易。 - 也许是因为感觉功能/LINQ 不够。
- 也许是因为它不可链接。(
yield
在执行操作后制作每个项目的可链接版本很容易,但这种行为并不是特别直观。)
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);
}
}