多个List 或 Array 进行 合并

1.调用

var aaa = new List<string>() { "0" };

var a1 = new List<string>() { "1" };
var a2 = new List<string>() { "2" };
var a3 = new List<string>() { "3" };
var a4 = new List<string>() { "4" };
var a5 = new List<string>() { "5" };

var b = aaa.MyConcat(a1, a2, a3, a4, a5);
int count = b.Count();

2.方法

/// <summary>
/// 扩展方法必须在静态类中
/// </summary>
public static class Test
{
    /// <summary>
    /// 拼接(扩展方法)  只要是 实现了 System.Collections.IEnumerable 接口 的都可以调用. 比如list ,Array 等等,下面的图片是举例
    /// </summary> 
    public static IEnumerable<TSource> MyConcat<TSource>(
       this IEnumerable<TSource> thisEnumerable,
       IEnumerable<TSource> one,
       IEnumerable<TSource> two,
       IEnumerable<TSource> three,
       IEnumerable<TSource> four,
       IEnumerable<TSource> five)
    {
        foreach (var item in thisEnumerable) yield return item;
        foreach (var item in one) yield return item;
        foreach (var item in two) yield return item;
        foreach (var item in three) yield return item;
        foreach (var item in four) yield return item;
        foreach (var item in five) yield return item;
        
        //按照实际需求继续写下去,也可以 用重载, 
        //System.Linq.Enumerable 类库中有1个参数的扩展方法,叫做 Concat, 
        //所以 把方法名 写成Concat 然后 写出 23456个参数的扩展方法.就可以了.
    }
}
举例

在这里插入图片描述

3.结果

在这里插入图片描述

在这里插入图片描述

转载于:https://www.cnblogs.com/xxxxue/p/11153591.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值