LINQ equivalent of foreach for IEnumerable

后端 未结 22 2291
夕颜
夕颜 2020-11-21 22:54

I\'d like to do the equivalent of the following in LINQ, but I can\'t figure out how:

IEnumerable items = GetItems();
items.ForEach(i => i.DoS         


        
22条回答
  •  遥遥无期
    2020-11-21 23:21

    As numerous answers already point out, you can easily add such an extension method yourself. However, if you don't want to do that, although I'm not aware of anything like this in the BCL, there's still an option in the System namespace, if you already have a reference to Reactive Extension (and if you don't, you should have):

    using System.Reactive.Linq;
    
    items.ToObservable().Subscribe(i => i.DoStuff());
    

    Although the method names are a bit different, the end result is exactly what you're looking for.

提交回复
热议问题