C# Linq 风格“为每个”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1509442/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:17:01  来源:igfitidea点击:

Linq style "For Each"

c#.netlinqforeach

提问by Stefan Steinegger

Is there any Linq style syntax for "For each" operations?

“对于每个”操作是否有任何 Linq 样式语法?

For instance, add values based on one collection to another, already existing one:

例如,将基于一个集合的值添加到另一个已经存在的集合中:

IEnumerable<int> someValues = new List<int>() { 1, 2, 3 };

IList<int> list = new List<int>();

someValues.ForEach(x => list.Add(x + 1));

Instead of

代替

foreach(int value in someValues)
{
  list.Add(value + 1);
}

采纳答案by Mark Seemann

Using the ToList() extension method is your best option:

使用 ToList() 扩展方法是您的最佳选择:

someValues.ToList().ForEach(x => list.Add(x + 1));

There is no extension method in the BCL that implements ForEach directly.

BCL 中没有直接实现 ForEach 的扩展方法。



Although there's no extension method in the BCLthat does this, there isstill an option in the Systemnamespace... if you add Reactive Extensionsto your project:

虽然有一个在没有扩展方法BCL做这个,也就是还处于一个选项System命名空间......如果你添加无扩展到您的项目:

using System.Reactive.Linq;

someValues.ToObservable().Subscribe(x => list.Add(x + 1));

This has the same end result as the above use of ToList, but is (in theory) more efficient, because it streams the values directly to the delegate.

这与上面使用 的最终结果相同ToList,但(理论上)更有效,因为它将值直接流式传输到委托。

回答by R. Martinho Fernandes

There isn't anything like that in standard Linq, but there is a ForEach operator in MoreLinq.

在标准 Linq 中没有类似的东西,但在MoreLinq 中有一个 ForEach 运算符。

回答by Noldorin

The Arrayand List<T>classes already have ForEachmethods, though only this specific implementation. (Note that the former is static, by the way).

ArrayList<T>班已经有ForEach方法,但只有该种具体实施。(static顺便说一下,请注意前者是)。

Not sure it really offers a great advantage over a foreachstatement, but you could write an extension method to do the job for all IEnumerable<T>objects.

不确定它是否真的比foreach语句提供了很大的优势,但是您可以编写一个扩展方法来为所有IEnumerable<T>对象完成这项工作。

public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
{
    foreach (var item in source)
        action(item);
}

This would allow the exact code you posted in your question to work just as you want.

这将允许您在问题中发布的确切代码按照您的意愿工作。

回答by LukeH

There isn't anything built-in, but you can easily create your own extension method to do it:

没有内置任何东西,但您可以轻松创建自己的扩展方法来做到这一点:

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

回答by stusmith

The official MS line is "because it's not a functional operation" (ie it's a stateful operation).

官方的 MS 行是“因为它不是功能性操作”(即它是有状态操作)。

Couldn't you do something like:

你不能做这样的事情:

list.Select( x => x+1 )

or if you really need it in a List:

或者如果你真的需要它在列表中:

var someValues = new List<int>( list.Select( x => x+1 ) );

回答by dustyburwell

There is no Linq ForEach extension. However, the Listclass has a ForEach method on it, if you're willing to use the Listdirectly.

没有 Linq ForEach 扩展。但是,List如果您愿意List直接使用,该类有一个 ForEach 方法。

For what it's worth, the standard foreachsyntax will give you the results you want and it's probably easier to read:

就其价值而言,标准foreach语法将为您提供所需的结果,并且可能更易于阅读:

foreach (var x in someValues)
{
    list.Add(x + 1);
}

If you're adamant you want an Linq style extension. it's trivial to implement this yourself.

如果你坚持你想要一个 Linq 风格的扩展。自己实现这一点很简单。

public static void ForEach<T>(this IEnumerable<T> @this, Action<T> action)
{
   foreach (var x in @this)
      action(x);
}