LINQ side effects

后端 未结 5 1492
别那么骄傲
别那么骄傲 2020-12-11 02:43

is it possible to substitute a foreach loop with a lambda expression in LINQ (.Select))?

List l = {1, 2, 3, 4, 5};
forea         


        
5条回答
  •  醉话见心
    2020-12-11 03:23

    There is no Linq equivalent of foreach, although it is fairly easy to implement one yourself.

    Eric Lippert gives a good description here of why this was not implemented in Linq itself.

    However, if your collection is a List (which it appears to be in your example), you can use List.ForEach:

    myList.ForEach(item => Console.WriteLine(item));
    

提交回复
热议问题