是否有Linq方法将单个项添加到IEnumerable <T>?

Joa*_*nge 54 .net c# ienumerable

基本上我想尝试做这样的事情:

image.Layers
Run Code Online (Sandbox Code Playgroud)

除了图层之外的所有图层都返回IEnumerable Parent,但在某些情况下,我只想这样做:

image.Layers.With(image.ParentLayer);
Run Code Online (Sandbox Code Playgroud)

因为它只在少数几个地方使用,相比之下,通常使用的100s image.Layers.这就是为什么我不想创建另一个也返回Parent图层的属性.

Ani*_*Ani 52

一种方法是从项目(例如数组)中创建单个序列,然后将Concat其创建到原始序列上:

image.Layers.Concat(new[] { image.ParentLayer } )
Run Code Online (Sandbox Code Playgroud)

如果您经常这样做,请考虑编写一个Append(或类似的)扩展方法,例如此处列出的方法,它可以让您:

image.Layers.Append(image.ParentLayer)
Run Code Online (Sandbox Code Playgroud)

  • 这个答案已经过时了。从.NET 4.7.1,.NET Core和.NET Standard开始,只需使用[`Append`](https://docs.microsoft.com/zh-cn/dotnet/api/system.linq.enumerable.append )。 (2认同)

rea*_*art 26

已经给出了许多实现.我看起来有点不同(但表现同样好)

此外,我发现它也可以控制ORDER.因此,我经常使用ConcatTo方法,将新元素放在前面.

public static class Utility
{
    /// <summary>
    /// Adds the specified element at the end of the IEnummerable.
    /// </summary>
    /// <typeparam name="T">The type of elements the IEnumerable contans.</typeparam>
    /// <param name="target">The target.</param>
    /// <param name="item">The item to be concatenated.</param>
    /// <returns>An IEnumerable, enumerating first the items in the existing enumerable</returns>
    public static IEnumerable<T> ConcatItem<T>(this IEnumerable<T> target, T item)
    {
        if (null == target) throw new ArgumentException(nameof(target));
        foreach (T t in target) yield return t;
        yield return item;
    }

    /// <summary>
    /// Inserts the specified element at the start of the IEnumerable.
    /// </summary>
    /// <typeparam name="T">The type of elements the IEnumerable contans.</typeparam>
    /// <param name="target">The IEnummerable.</param>
    /// <param name="item">The item to be concatenated.</param>
    /// <returns>An IEnumerable, enumerating first the target elements, and then the new element.</returns>
    public static IEnumerable<T> ConcatTo<T>(this IEnumerable<T> target, T item)
    {
        if (null == target) throw new ArgumentException(nameof(target));
        yield return item;
        foreach (T t in target) yield return t;
    }
}
Run Code Online (Sandbox Code Playgroud)

或者,使用隐式创建的数组.(使用params关键字)这样您就可以调用方法一次添加一个或多个项目:

public static class Utility
{
    /// <summary>
    /// Adds the specified element at the end of the IEnummerable.
    /// </summary>
    /// <typeparam name="T">The type of elements the IEnumerable contans.</typeparam>
    /// <param name="target">The target.</param>
    /// <param name="items">The items to be concatenated.</param>
    /// <returns>An IEnumerable, enumerating first the items in the existing enumerable</returns>
    public static IEnumerable<T> ConcatItems<T>(this IEnumerable<T> target, params T[] items) =>
        (target ?? throw new ArgumentException(nameof(target))).Concat(items);

    /// <summary>
    /// Inserts the specified element at the start of the IEnumerable.
    /// </summary>
    /// <typeparam name="T">The type of elements the IEnumerable contans.</typeparam>
    /// <param name="target">The IEnummerable.</param>
    /// <param name="items">The items to be concatenated.</param>
    /// <returns>An IEnumerable, enumerating first the target elements, and then the new elements.</returns>
    public static IEnumerable<T> ConcatTo<T>(this IEnumerable<T> target, params T[] items) =>
        items.Concat(target ?? throw new ArgumentException(nameof(target)));
Run Code Online (Sandbox Code Playgroud)

  • +1用于提供不必要地分配新列表以连接单个元素的答案.并提供更多的灵活性! (2认同)

cbp*_*cbp 13

AppendPrepend现在已经被添加到.NET标准框架,让您不再需要自己编写.只需这样做:

image.Layers.Append(image.ParentLayer)
Run Code Online (Sandbox Code Playgroud)

请参阅.Net Framework 2.0中的43个API,但.Net Framework 4.6.1中没有这些API?获得一系列新功能.

  • 该答案应该被赞成,以便人们可以轻松地克服所有过时的答案 (2认同)

Jar*_*Par 12

没有一种方法可以做到这一点.最接近的是Enumerable.Concat方法,但尝试将a IEnumerable<T>与另一个组合IEnumerable<T>.您可以使用以下内容使其适用于单个元素

image.Layers.Concat(new [] { image.ParentLayer });
Run Code Online (Sandbox Code Playgroud)

或者只是添加一个新的扩展方法

public static IEnumerable<T> ConcatSingle<T>(this IEnumerable<T> enumerable, T value) {
  return enumerable.Concat(new [] { value });
}
Run Code Online (Sandbox Code Playgroud)


Ree*_*sey 7

您可以使用Enumerable.Concat:

var allLayers = image.Layers.Concat(new[] {image.ParentLayer});
Run Code Online (Sandbox Code Playgroud)


the*_*oop 5

你可以这样做:

image.Layers.Concat(new[] { image.ParentLayer });
Run Code Online (Sandbox Code Playgroud)

它使用包含要添加的内容的单元素数组来连接枚举


Ger*_*old 5

我曾经为此做过一个很好的小功能:

public static class CoreUtil
{    
    public static IEnumerable<T> AsEnumerable<T>(params T[] items)
    {
        return items;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在这是可能的:

image.Layers.Append(CoreUtil.AsEnumerable(image.ParentLayer, image.AnotherLayer))
Run Code Online (Sandbox Code Playgroud)