使用的Foreach条款LA​​MBDA防爆pression条款、Foreach、LA、pression

2023-09-07 01:20:34 作者:方式干净

可能重复:   Why是不是有IEnumerable接口上一个foreach扩展方法?

修改

有关引用,这里的博客文章中评论这埃里克referrrred到

http://blogs.msdn.com/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx

更多的是好奇,我想,但一个是C#规格学者...

为什么是它给ForEach()子句不工作(或不可用)对IQueryable的使用/ IEnumerable的结果集...

您必须首先将你的结果了ToList()或的ToArray() presumably那里有一个技术限制的方式C#迭代IEnumerables比。列表... 它是什么做的延迟执行的的IEnumerables / IQuerable集合。 例如,

  VAR userAgentStrings = uasdc.UserAgentStrings
    。凡< UserAgentString>(P => p.DeviceID == 0安培;&安培;
                            !p.UserAgentString1.Contains(MSIE));
//作品
。userAgentStrings.ToList()的ForEach(UAS => ProcessUserAgentString(UAS));

//作品
Array.ForEach(userAgentStrings.ToArray(),UAS => ProcessUserAgentString(UAS));

//不起作用
userAgentStrings.ForEach(UAS => ProcessUserAgentString(UAS));
 

解决方案

这是多么惊人的巧合,我刚才写的这个问题非常的博客文章。它为 公布5月18日。没有任何技术原因,我们(或者你!)无法做到这一点。究其原因,并非是哲学。见我下周的博客我的说法。

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

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...

Why is it that the ForEach() clause doesn't work (or isn't available) for use on IQueryable/IEnumerable result sets...

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.

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));

解决方案

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.