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

后端 未结 11 620
感动是毒
感动是毒 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-04 23:35

    There is no single method which does this. The closest is the Enumerable.Concat method but that tries to combine an IEnumerable with another IEnumerable. You can use the following to make it work with a single element

    image.Layers.Concat(new [] { image.ParentLayer });
    

    Or just add a new extension method

    public static IEnumerable ConcatSingle(this IEnumerable enumerable, T value) {
      return enumerable.Concat(new [] { value });
    }
    

提交回复
热议问题