[Solved] Most efficient way to concatenate list within list without a loop


List<int> theInts = unicorns.SelectMany(unicorn => unicorn.Numbers).ToList();

Without measuring, the only thing in here that gives me pause from a performance perspective is the .ToList

If there are a TON of numbers, it’s possible that ToList may repeatedly re-allocate its backing array. In order to escape this behavior, you have to have some idea about how many Numbers to expect.

List<int> theInts = new List<int>(expectedSize);
theInts.AddRange(unicorns.SelectMany(unicorn => unicorn.Numbers));

6

solved Most efficient way to concatenate list within list without a loop