// consider ALL items in A and B:
foreach( var item in AllCustomItems())
You can even make AllCustomItems accept a params
array of collections to iterate!
SUPER powerful way to process collections.
And Linq has even more, but I prefer to just write explicit code because I find it easier to debug.
Kurt-Dekker:
You can even make AllCustomItems accept a params
array of collections to iterate!
SUPER powerful way to process collections.
And Linq has even more, but I prefer to just write explicit code because I find it easier to debug.
Thanks! this is also very elegant and nicely readable 
Regarding the Linq lib you are mentioning - I think i came across some “Concat(listA, listB, listC)” function from linq on stack overfrlow, but i never managed to use it in Unity. I believe i added “using System.linq” or something like that but that wasn’t right. Could you maybe tell me what I need to do to use the linq functions ? 
Personally I wouldn’t bother with Linq for this. Linq is great if you want to make the syntax for doing stuff like adding conditional expressions to selections of items such as ensuring there’s no duplicates and/or having the resultant list ordered in a specific way. In the end, it’s syntactic sugar only. It can also produce a lot of garbage collection overhead depending what you’re doing.
“using System.Linq” is sufficient for most things yes.
Mostly it’s working with IEnumerable so “var result = ListA.ConCat(ListB).ToList()” sort of thing. The “ToList” or “ToArray” is needed because the result is another IEnumerable so if you specifically want the list functionality then you need to create a list.
Avoiding creating a new list as a result is good which is what Kurt was doing above in his “explicit sort of Linq” set-up.
If that waste doesn’t matter then have at it!
Well, adding the namespace using System.Linq;
at the top should be enough.
Though this namespace mainly contains classes with extension methods. So you usually have to call those methods on a collection / list / array. C# does not have the concept of standalone methods. Methods are always part of a class / struct / type. So you can not have a global static method.
There is the Concat method which does what you want. As you can see it’s an extension method that is defined in the class Enumerable in the System.Linq namespace. You can use it like this:
var listAll = listA.Concat(listB).Concat(listC).ToList();
This would create 2 IEnumerables which take care of concatting the elements of the 3 lists and in the end we create a new list out of that new collection.
Since Concat is an extension method, the above line actually reads like this:
var listAll = Enumerable.ToList(Enumerable.Concat(Enumerable.Concat(listA, ListB), listC));
I also tend to avoid linq because of the lacking control of garbage.
This ^ ^ ^ ^
My personal feeling is that Linq obfuscates code.
Regardless if that is true, if you cannot write the looping code long-hand (and I mean instantly and right off the top of your head, no hesitation, just bip-bip-bip write the code the way I did above), then you have NO BUSINESS trying to do it in Linq.
You have to understand your problem first. Linq will NOT help you understand any problem.