I am new to LINQ and i want to switch onto it from traditional loops I have an array of GameObjects and I just want to iterate over all of them and set one of their component enabled, and I wanna know if there is 1 line elegant way with LINQ to replace foreach or for loop's version
Asked
Active
Viewed 3,282 times
1
-
Sure. Actually linq is all about this. The method you are looking for is ForEach. But you need first to add using System.Linq. Anyway, I recommend you first to read about linq. – Abdullah Dibas Jul 13 '19 at 16:01
-
Before using `List
.ForEach()`, please take a moment to read through this article from a decade ago, still relevant: https://blogs.msdn.microsoft.com/ericlippert/2009/05/18/foreach-vs-foreach/ – Jesse C. Slicer Jul 13 '19 at 16:02 -
Yeah, it worked. Tanks – Chestera Jul 13 '19 at 16:02
1 Answers
1
gameObjects.ToList().ForEach(x => {do Stuff with list});
Some reading on forEach in C# however. Could be interesting: https://blogs.msdn.microsoft.com/ericwhite/2009/04/08/why-i-dont-use-the-foreach-extension-method/

AndrewG
- 145
- 1
- 13
-
so are you suggesting that using linq version of foreach is bad practice? – Chestera Jul 13 '19 at 16:08