I need to prepend a single value to an IEnumerable (in this case, IEnumerable<string[]>). In order to do that, I’m creating a List<T> just to wrap the first value so that I can call Concat:
// get headers and data together
IEnumerable<string[]> headers = new List<string[]> {
GetHeaders()
};
var all = headers.Concat(GetData());
Yuck. Is there a better way? And how would you handle the opposite case of appending a value?
I wrote custom extension methods to do this:
In your scenario, you would write:
As chilltemp commented, this does not mutate the original collection. In true Linq fashion, it generates a new
IEnumerable<T>.Note: An eager null argument check is recommended for
source, but not shown for brevity.