LINQ equivalent of foreach for IEnumerable

后端 未结 22 2554
夕颜
夕颜 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

    This "functional approach" abstraction leaks big time. Nothing on the language level prevents side effects. As long as you can make it call your lambda/delegate for every element in the container - you will get the "ForEach" behavior.

    Here for example one way of merging srcDictionary into destDictionary (if key already exists - overwrites)

    this is a hack, and should not be used in any production code.

    var b = srcDictionary.Select(
                                 x=>
                                    {
                                      destDictionary[x.Key] = x.Value;
                                      return true;
                                    }
                                 ).Count();
    

提交回复
热议问题