Linq

System.Linq.Zip

2021-11-05  本文已影响0人  呼啦啦啦就是飞

System.Linq.Zip函数作用为将两个序列对应元素合为一个元素

public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult>(this IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector);

注意:
参数first、second中first序列可以比second序列长或相等
示例:

List<string> stringList1 = new List<String> { "Freesc", "Joshua", "Ken" };
List<string> stringList2 = new List<String> { "Huang", "Guan", "Wang" };
List<string> resultList = new List<string>();
foreach (var item in stringList1.Zip(stringList2, (first, second) => first + "" + second)) {
    resultList.Add(item);
}

输出结果:
"FreescHuang"
"JoshuaGuan"
"KenWang"

示例:

List<string> stringList1 = new List<String> { "Freesc", "Joshua", "Ken" };
List<string> stringList2 = new List<String> { "Huang", "Guan" };
List<string> resultList = new List<string>();
foreach (var item in stringList1.Zip(stringList2, (first, second) => first + "" + second)) {
    resultList.Add(item);
}

输出结果:
"FreescHuang"
"JoshuaGuan"
"Ken"

上一篇 下一篇

猜你喜欢

热点阅读