本文介绍了对Enumerable< T>中的所有元素执行特定动作.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Enumerable<T>,并且正在寻找一种方法,该方法可以让我对每个元素执行操作,类似于Select,但有副作用.像这样:

I have an Enumerable<T> and am looking for a method that allows me to execute an action for each element, kind of like Select but then for side-effects. Something like:

string[] Names = ...;
Names.each(s => Console.Writeline(s));

Names.each(s => GenHTMLOutput(s));   
// (where GenHTMLOutput cannot for some reason receive the enumerable itself as a parameter)

我确实尝试过Select(s=> { Console.WriteLine(s); return s; }),但是它没有打印任何内容.

I did try Select(s=> { Console.WriteLine(s); return s; }), but it wasn't printing anything.

推荐答案

一种简单快捷的方法是:

A quick-and-easy way to get this is:

Names.ToList().ForEach(e => ...);

这篇关于对Enumerable&lt; T&gt;中的所有元素执行特定动作.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 16:05