LINQ equivalent of foreach for IEnumerable

后端 未结 22 2255
夕颜
夕颜 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:30

    Fredrik has provided the fix, but it may be worth considering why this isn't in the framework to start with. I believe the idea is that the LINQ query operators should be side-effect-free, fitting in with a reasonably functional way of looking at the world. Clearly ForEach is exactly the opposite - a purely side-effect-based construct.

    That's not to say this is a bad thing to do - just thinking about the philosophical reasons behind the decision.

提交回复
热议问题