# 将不同类型的集合与LINQ相结合Combining collections of different types with LINQ

``````const int length = 5;
var ints = new int[length] { 1, 2, 3, 4, 5 };
var strings = new string[length] { "s1", "s2", "s3", "s4", "s5" };
``````

``````for (var index = 0; index < length; index++)
{
var i = ints[index];
var s = strings[index];
DoSomething(i, s);
}
``````

``````var items = ... i in ints
... s in strings
select new { I = i, S = s };

foreach (var item in items)
{
DoSomething(item.I, item.S);
}
``````

``````var items = from i in ints
from s in strings
select new { I = i, S = s };
``````

## 4 个回复4

### ===============>>#1 票数：5

``````var ints = new int[] { 1, 2, 3, 4, 5, 6 };
var strings = new string[] { "s1", "s2", "s3", "s4", "s5" };
var zipper = ints.Zip(strings, (i, s) => new {i, s});
``````

### ===============>>#2 票数：5

``````var items = ints.Zip(strings, (i, s) => new {I = i, S = s});
``````

### ===============>>#3 票数：3

``````ints.Zip(strings, (i,s) => DoSomething(i,s));
``````

``````ints.Zip(strings, (i,s) => { DoSomething(i,s); return 0;} );
``````

``````var a = ints.Zip(strings, (i,s) => { DoSomething(i,s); return 0;} );
``````

``````a.ToList();
``````

``````a.Count();
``````

``````ints.Zip(strings, (i,s) => { DoSomething(i,s); return 0;} ).Count();
``````

### ===============>>#4 票数：2 已采纳

``````ints.Select((x,idx) => new { I = x, S = strings[idx] });
``````