如何合并两个集合到一个 [英] How to merge two collections into one
问题描述
我在Silverlight创建了两个集合类,每个集合在不同时间用不同的方法创建的。
I have two collections created within Silverlight each collection is created at separate times by different methods.
方法1创建一个List<>标题的人(包含字段名,姓氏岁)
Method 1 creates a List<> titled person ( contains fields first name, last name age)
2的方法创建一个List&LT;>标题电话(包括手机号码,手机号码)
Method 2 created a List<> titled Phone ( contains phone number, cell number)
有内SL的方式这两个词典组合成一个集合,然后可以绑定到视图显示?
Is there a way within SL to combine these two Lists into one Collection that could then be bound to the view for display?
例:组合成一个包含所有属性(姓,名年龄,电话号码,手机号码)的集合
Example: combined into a collection that contained all properties (first name, last name age, phone number, cell number)
推荐答案
您正在寻找的邮编
功能。它可以让你2集组合成一个单一结合eache的元素。
You're looking for the Zip
function. It allows you to combine 2 collections into a single one by combining the elements of eache.
List<Type1> col1 = ...;
List<Type2> col2 = ...;
var combined = col1.Zip(col2, (x, y) => new {
FirstName = x.FirstName,
LastName = x.LastName,
PhoneNumber = y.PhoneNumber,
CellNumber = y.CellNumber });
我不知道如果Zip是可以在没有在Silverlight中。如果没有那么这里是它的一个定义
I'm not sure if Zip is available on not in Silverlight. If not then here's a definition of it
public static IEnumerable<TRet> Zip<T1, T2, TRet>(
this IEnumerable<T1> enumerable1,
IEnumerable<T2> enumreable2,
Func<T1, T2, TRet> func) {
using (var e1 = enumerable1.GetEnumerator()) {
using (var e2 = enumerable2.GetEnumerator()) {
while (e1.MoveNext() && e2.MoveNext()) {
yield return func(e1.Current, e2.Current);
}
}
}
}
这篇关于如何合并两个集合到一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!