Is there a Linq method to add a single item to an IEnumerable?

后端 未结 11 662
感动是毒
感动是毒 2021-02-04 23:10

Basically I am trying to do something like this:

image.Layers

which returns an IEnumerable for all layers except the Parent layer,

11条回答
  •  难免孤独
    2021-02-05 00:01

    I use the following extension methods to avoid creating a useless Array:

    public static IEnumerable ConcatSingle(this IEnumerable enumerable, T value) {
       return enumerable.Concat(value.Yield());
    }
    
    public static IEnumerable Yield(this T item) {
        yield return item;
    }
    

提交回复
热议问题