我想在LINQ中执行以下等效操作,但我无法确定如何执行:
IEnumerable<Item> items = GetItems();
items.ForEach(i => i.DoStuff());
真正的语法是什么?
我想在LINQ中执行以下等效操作,但我无法确定如何执行:
IEnumerable<Item> items = GetItems();
items.ForEach(i => i.DoStuff());
真正的语法是什么?
当前回答
这么多的答案,但所有人都未能找到自定义通用ForEach扩展的一个非常重要的问题:性能!更具体地说,内存使用和GC。
考虑以下示例。针对.NET Framework 4.7.2或.NET Core 3.1.401,配置为Release,平台为Any CPU。
public static class Enumerables
{
public static void ForEach<T>(this IEnumerable<T> @this, Action<T> action)
{
foreach (T item in @this)
{
action(item);
}
}
}
class Program
{
private static void NoOp(int value) {}
static void Main(string[] args)
{
var list = Enumerable.Range(0, 10).ToList();
for (int i = 0; i < 1000000; i++)
{
// WithLinq(list);
// WithoutLinqNoGood(list);
WithoutLinq(list);
}
}
private static void WithoutLinq(List<int> list)
{
foreach (var item in list)
{
NoOp(item);
}
}
private static void WithLinq(IEnumerable<int> list) => list.ForEach(NoOp);
private static void WithoutLinqNoGood(IEnumerable<int> enumerable)
{
foreach (var item in enumerable)
{
NoOp(item);
}
}
}
乍一看,这三种变体都应该表现得同样出色。然而,当ForEach扩展方法被多次调用时,最终会产生垃圾,这意味着代价高昂的GC。事实上,在热路径上使用此ForEach扩展方法已被证明会完全破坏循环密集型应用程序的性能。
类似地,弱类型的foreach循环也会产生垃圾,但它仍然比foreach扩展更快,占用的内存更少(它也会受到委托分配的影响)。
强类型foreach:内存使用
弱类型foreach:内存使用
ForEach扩展:内存使用
分析
对于强类型foreach,编译器可以使用类的任何优化枚举器(例如基于值的),而泛型foreach扩展必须返回到将在每次运行时分配的泛型枚举器。此外,实际代表还将意味着额外分配。
使用WithoutLinqNoGood方法也会得到类似的坏结果。在那里,参数的类型是IEnumerable<int>,而不是List<int>。这意味着相同类型的枚举器分配。
以下是IL中的相关差异。基于值的枚举器当然更可取!
IL_0001: callvirt instance class
[mscorlib]System.Collections.Generic.IEnumerator`1<!0>
class [mscorlib]System.Collections.Generic.IEnumerable`1<!!T>::GetEnumerator()
vs
IL_0001: callvirt instance valuetype
[mscorlib]System.Collections.Generic.List`1/Enumerator<!0>
class [mscorlib]System.Collections.Generic.List`1<int32>::GetEnumerator()
结论
OP询问如何在IEnumerable<T>上调用ForEach()。最初的答案清楚地表明了如何做到这一点。当然你能做到,但再说一遍;我的回答清楚地表明你不应该。
在针对.NET Core 3.1.401(使用Visual Studio 16.7.2编译)时验证了相同的行为。
其他回答
我采用了Fredrik的方法并修改了返回类型。
这样,该方法与其他LINQ方法一样支持延迟执行。
EDIT:如果这一点不清楚,则此方法的任何用法都必须以ToList()或任何其他方式结束,以强制该方法处理完整的可枚举对象。否则,将不会执行该操作!
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> enumeration, Action<T> action)
{
foreach (T item in enumeration)
{
action(item);
yield return item;
}
}
下面是帮助您了解它的测试:
[Test]
public void TestDefferedExecutionOfIEnumerableForEach()
{
IEnumerable<char> enumerable = new[] {'a', 'b', 'c'};
var sb = new StringBuilder();
enumerable
.ForEach(c => sb.Append("1"))
.ForEach(c => sb.Append("2"))
.ToList();
Assert.That(sb.ToString(), Is.EqualTo("121212"));
}
如果最后删除ToList(),则会看到测试失败,因为StringBuilder包含一个空字符串。这是因为没有方法强制ForEach枚举。
Fredrik提供了解决方案,但可能值得考虑一下为什么这不在框架中。我认为,LINQ查询运算符应该是无副作用的,符合合理的功能性世界观。很明显,ForEach恰恰相反——一个纯粹基于副作用的构造。
这并不是说这是一件坏事——只是想想这个决定背后的哲学原因。
2012年7月17日更新:显然,从C#5.0开始,下面描述的foreach的行为已经改变,“在嵌套lambda表达式中使用foreach迭代变量不再产生意外结果。”这个答案不适用于C#≥5.0。
@约翰·斯基特和所有喜欢foreach关键字的人。
5.0之前的C#中的“foreach”的问题是,它与其他语言中等效的“for understanding”的工作方式不一致,也与我期望它的工作方式(此处陈述的个人观点仅是因为其他人提到了他们对可读性的看法)不一致。查看有关“修改关闭的访问”的所有问题以及“关闭被认为有害的循环变量”。这只是“有害的”,因为“foreach”是在C#中实现的。
使用@Fredrik Kalseth的答案中功能等效的扩展方法,以下面的示例为例。
public static class Enumerables
{
public static void ForEach<T>(this IEnumerable<T> @this, Action<T> action)
{
foreach (T item in @this)
{
action(item);
}
}
}
为这个过于做作的例子道歉。我只使用Observable,因为这样做并不完全牵强。显然,有更好的方法来创建这种可观察性,我只是试图证明一点。通常,订阅可观察到的代码是异步执行的,并且可能在另一个线程中执行。如果使用“foreach”,这可能会产生非常奇怪且可能不确定的结果。
使用“ForEach”扩展方法的以下测试通过:
[Test]
public void ForEachExtensionWin()
{
//Yes, I know there is an Observable.Range.
var values = Enumerable.Range(0, 10);
var observable = Observable.Create<Func<int>>(source =>
{
values.ForEach(value =>
source.OnNext(() => value));
source.OnCompleted();
return () => { };
});
//Simulate subscribing and evaluating Funcs
var evaluatedObservable = observable.ToEnumerable().Select(func => func()).ToList();
//Win
Assert.That(evaluatedObservable,
Is.EquivalentTo(values.ToList()));
}
以下操作失败并出现错误:
应为:相当于<0,1,2,3,4,5,6,7,8,9>但是:<9,9,9
[Test]
public void ForEachKeywordFail()
{
//Yes, I know there is an Observable.Range.
var values = Enumerable.Range(0, 10);
var observable = Observable.Create<Func<int>>(source =>
{
foreach (var value in values)
{
//If you have resharper, notice the warning
source.OnNext(() => value);
}
source.OnCompleted();
return () => { };
});
//Simulate subscribing and evaluating Funcs
var evaluatedObservable = observable.ToEnumerable().Select(func => func()).ToList();
//Fail
Assert.That(evaluatedObservable,
Is.EquivalentTo(values.ToList()));
}
让你的副作用远离我的IEnumerable
我想在LINQ中执行以下等效操作,但我无法确定如何执行:
正如其他人在国内外指出的,LINQ和IEnumerable方法预计不会产生副作用。
你真的想对IEnumerable中的每个项目“做点什么”吗?那么foreach是最好的选择。当这里出现副作用时,人们并不感到惊讶。
foreach (var i in items) i.DoStuff();
我打赌你不想有副作用
然而,根据我的经验,通常不需要副作用。通常情况下,有一个简单的LINQ查询等待被发现,并伴随着Jon Skeet、Eric Lippert或Marc Gravell的StackOverflow.com回答,解释如何做您想要的事情!
一些示例
如果您实际上只是在聚合(累积)一些值,那么应该考虑聚合扩展方法。
items.Aggregate(initial, (acc, x) => ComputeAccumulatedValue(acc, x));
也许您想从现有值创建一个新的IEnumerable。
items.Select(x => Transform(x));
或者你想创建一个查找表:
items.ToLookup(x, x => GetTheKey(x))
可能性的列表(双关语并非完全有意)还在继续。
ForEach也可以链接,只需在动作后放回桩线即可。保持流利
Employees.ForEach(e=>e.Act_A)
.ForEach(e=>e.Act_B)
.ForEach(e=>e.Act_C);
Orders //just for demo
.ForEach(o=> o.EmailBuyer() )
.ForEach(o=> o.ProcessBilling() )
.ForEach(o=> o.ProcessShipping());
//conditional
Employees
.ForEach(e=> { if(e.Salary<1000) e.Raise(0.10);})
.ForEach(e=> { if(e.Age >70 ) e.Retire();});
实现的一个版本。
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> enu, Action<T> action)
{
foreach (T item in enu) action(item);
return enu; // make action Chainable/Fluent
}
编辑:Lazy版本使用的是收益率返回,如下所示。
public static IEnumerable<T> ForEachLazy<T>(this IEnumerable<T> enu, Action<T> action)
{
foreach (var item in enu)
{
action(item);
yield return item;
}
}
Lazy版本需要具体化,例如ToList(),否则什么都不会发生。请参阅以下ToolmakerSteve的精彩评论。
IQueryable<Product> query = Products.Where(...);
query.ForEachLazy(t => t.Price = t.Price + 1.00)
.ToList(); //without this line, below SubmitChanges() does nothing.
SubmitChanges();
我将ForEach()和ForEachLazy()都保存在库中。