http://xiangai.taobao.com
http://shop148612228.taobao.com

Enumerable.Zip(Of TFirst, TSecond, TResult) 方法 Linq zip 方法

Enumerable.Zip(Of TFirst, TSecond, TResult) 方法

 

 

通过使用指定的谓词函数合并两个序列。

 

命名空间: System.Linq 程序集: System.Core(在 System.Core.dll 中)
复制
public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult>(
	this IEnumerable<TFirst> first,
	IEnumerable<TSecond> second,
	Func<TFirst, TSecond, TResult> resultSelector
)

类型参数

TFirst

第一个输入序列中的元素的类型。

TSecond

第二个输入序列中的元素的类型。

TResult

结果序列的元素的类型。

参数

first
类型:System.Collections.Generic.IEnumerable(Of TFirst) 要合并的第一个序列。
second
类型:System.Collections.Generic.IEnumerable(Of TSecond) 要合并的第二个序列。
resultSelector
类型:System.Func(Of TFirst, TSecond, TResult) 用于指定如何合并这两个序列的元素的函数。

返回值

类型:System.Collections.Generic.IEnumerable(Of TResult) 一个 IEnumerable(Of T),包含两个输入序列的已合并元素。

使用说明

在 Visual Basic 和 C# 中,可以在 IEnumerable(Of TFirst) 类型的任何对象上将此方法作为实例方法来调用。当使用实例方法语法调用此方法时,请省略第一个参数。有关更多信息,请参见扩展方法 (Visual Basic)扩展方法(C# 编程指南)
异常条件
ArgumentNullException

firstsecondNothing

此方法通过使用延迟执行实现。即时返回值为一个对象,该对象存储执行操作所需的所有信息。只有通过直接调用对象的 GetEnumerator 方法或使用 Visual C# 中的 foreach(或 Visual Basic 中的 For Each)来枚举该对象时,才执行此方法表示的查询。

该方法将第一个序列中的每个元素与第二个序列中有相同索引的元素合并。如果该序列不具有相同数目的元素,则直到它到达其中一个的末尾,该方法才合并序列。例如,如果一个序列有三个元素,另一个序列具有四个元素,那么结果序列将只有三个元素。

下面的代码示例演示如何使用 Zip(Of TFirst, TSecond, TResult) 方法来合并两个序列。

复制
            int[] numbers = { 1, 2, 3, 4 };
            string[] words = { "one", "two", "three" };

            var numbersAndWords = numbers.Zip(words, (first, second) => first + " " + second);

            foreach (var item in numbersAndWords)
                Console.WriteLine(item);

            // This code produces the following output:

            // 1 one
            // 2 two
            // 3 three

posted @ 2012-01-16 23:16  万事俱备就差个程序员  阅读(1277)  评论(1编辑  收藏  举报

http://xiangai.taobao.com
http://shop148612228.taobao.com
如果您觉得对您有帮助.领个红包吧.谢谢.
支付宝红包
微信打赏 支付宝打赏