我有两个ICollection,我想要加入联盟。目前,我使用foreach循环来做这件事,但这感觉很冗长和可怕。Java的addAll()的C#等价物是什么
此问题的示例:
ICollection<IDictionary<string, string>> result = new HashSet<IDictionary<string, string>>();
// ...
ICollection<IDictionary<string, string>> fromSubTree = GetAllTypeWithin(elementName, element);
foreach( IDictionary<string, string> dict in fromSubTree ) { // hacky
result.Add(dict);
}
// result is now the union of the two sets发布于 2010-03-18 02:22:07
您可以使用Enumerable.Union扩展方法:
result = result.Union(fromSubTree).ToList();由于result被声明为ICollection<T>,因此需要使用ToList()调用将结果IEnumerable<T>转换为List<T> (实现ICollection<T>)。如果枚举是可接受的,您可以停止ToList()调用,并获得延迟执行(如果需要)。
发布于 2013-05-10 05:22:39
AddRange()会将源列表追加到另一个列表的末尾,这可能会满足您的需要。
destList.AddRange(srcList);发布于 2010-03-18 02:21:58
LINQ的Enumerable.Union将会工作:
https://stackoverflow.com/questions/2464767
复制相似问题