C#Linq Zip方法

发布时间 2023-11-24 19:09:20作者: 漫思

使用Linq中的Zip方法,进行合并序列

定义两个数组:

 int[] intArray = { 1, 2, 3, 5, 6 };
 string[] stringArray = { "A", "B", "C", "D", "E", "F" };

然后用Zip方法合并两个数组

var mergedSed = intArray.AsQueryable().Zip(stringArray, (one, two) => one + "_" + two);

示例代码如下:

using System;
using System.Linq;

namespace _001Linq_Zip方法
{
    class Program
    {
        static void Main(string[] args)
        {
            //数组
            int[] intArray = { 1, 2, 3, 5, 6 };
            string[] stringArray = { "A", "B", "C", "D", "E", "F" };
            var mergedSed = intArray.AsQueryable().Zip(stringArray, (one, two) => one + "_" + two);
            foreach (var item in mergedSed)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
    }
}

运行结果如下:

5a5fa2264b8f448dbdbdd1a5b5093465.png (1837×618) (csdnimg.cn)