For example:
string element = 'a'; IEnumerable<string> list = new List<string>{ 'b', 'c', 'd' }; IEnumerable<string> singleList = ???; //singleList yields 'a', 'b', 'c', 'd'
I take it you can't just Insert
into the existing list?
Well, you could use new[] {element}.Concat(list)
.
Otherwise, you could write your own extension method:
public static IEnumerable<T> Prepend<T>( this IEnumerable<T> values, T value) { yield return value; foreach (T item in values) { yield return item; } } ... var singleList = list.Prepend("a");
Since .NET framework 4.7.1 there is LINQ method for that:
list.Prepend("a");
https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.prepend?view=netframework-4.7.1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With