C# 合并两个 Collection<T>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/56078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Merging two Collection<T>
提问by Michael Stum
I got a Function that returns a Collection<string>
, and that calls itself recursively to eventually return one big Collection<string>
.
我得到了一个返回 a 的函数,Collection<string>
它递归地调用自己以最终返回一个 big Collection<string>
。
Now, i just wonder what the best approach to merge the lists? Collection.CopyTo()
only copies to string[], and using a foreach()
loop feels like being inefficient. However, since I also want to filter out duplicates, I feel like i'll end up with a foreach that calls Contains()
on the Collection
.
现在,我只是想知道合并列表的最佳方法是什么?Collection.CopyTo()
只复制到 string[],使用foreach()
循环感觉效率低下。然而,因为我也想筛选出重复的,我觉得我会在foreach结束的呼叫Contains()
上Collection
。
I wonder, is there a more efficient way to have a recursive function that returns a list of strings without duplicates? I don't have to use a Collection
, it can be pretty much any suitable data type.
我想知道,有没有更有效的方法来让递归函数返回一个没有重复的字符串列表?我不必使用 a Collection
,它几乎可以是任何合适的数据类型。
Only exclusion, I'm bound to Visual Studio 2005 and .net 3.0, so no LINQ.
只是排除,我必须使用 Visual Studio 2005 和 .net 3.0,所以没有 LINQ。
Edit:To clarify: The Function takes a user out of Active Directory, looks at the Direct Reports of the user, and then recursively looks at the direct reports of every user. So the end result is a List of all users that are in the "command chain" of a given user.Since this is executed quite often and at the moment takes 20 Seconds for some users, i'm looking for ways to improve it. Caching the result for 24 Hours is also on my list btw., but I want to see how to improve it before applying caching.
编辑:澄清:该功能将用户从 Active Directory 中取出,查看用户的直接下属,然后递归查看每个用户的直接下属。所以最终结果是给定用户的“命令链”中的所有用户的列表。由于它经常执行,并且目前某些用户需要 20 秒,我正在寻找改进它的方法。顺便说一句,缓存 24 小时的结果也在我的列表中,但我想在应用缓存之前看看如何改进它。
采纳答案by Mendelt
If you're using List<> you can use .AddRange to add one list to the other list.
如果您使用的是 List<>,您可以使用 .AddRange 将一个列表添加到另一个列表。
Or you can use yield return to combine lists on the fly like this:
或者您可以使用 yield return 来动态组合列表,如下所示:
public IEnumerable<string> Combine(IEnumerable<string> col1, IEnumerable<string> col2)
{
foreach(string item in col1)
yield return item;
foreach(string item in col2)
yield return item;
}
回答by jfs
I think HashSet<T>
is a great help.
我认为HashSet<T>
是一个很大的帮助。
The
HashSet<T>
class provides high performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.
本
HashSet<T>
类提供高性能的一组操作。集合是不包含重复元素且其元素没有特定顺序的集合。
Just add items to it and then use CopyTo.
只需向其中添加项目,然后使用 CopyTo。
Update: HashSet<T>
is in .Net 3.5
更新:HashSet<T>
在 .Net 3.5 中
Maybe you can use Dictionary<TKey, TValue>
. Setting a duplicate key to a dictionary will not raise an exception.
也许你可以使用Dictionary<TKey, TValue>
. 为字典设置重复键不会引发异常。
回答by Jon Limjap
You might want to take a look at Iesi.Collectionsand Extended Generic Iesi.Collections(because the first edition was made in 1.1 when there were no generics yet).
您可能想看看Iesi.Collections和Extended Generic Iesi.Collections(因为第一版是在 1.1 中制作的,当时还没有泛型)。
Extended Iesi has an ISet class which acts exactly as a HashSet: it enforces unique members and does not allow duplicates.
Extended Iesi 有一个 ISet 类,它的作用与 HashSet 完全相同:它强制执行唯一成员并且不允许重复。
The nifty thing about Iesi is that it has set operators instead of methods for merging collections, so you have the choice between a union (|), intersection (&), XOR (^) and so forth.
Iesi 的妙处在于它具有集合运算符而不是用于合并集合的方法,因此您可以在并集 (|)、交集 (&)、异或 (^) 等之间进行选择。
回答by Matthew M. Osborn
Can you pass the Collection into you method by refernce so that you can just add items to it, that way you dont have to return anything. This is what it might look like if you did it in c#.
你能不能通过引用将 Collection 传递给你的方法,这样你就可以向它添加项目,这样你就不必返回任何东西。如果您在 c# 中完成它,它可能看起来像这样。
class Program
{
static void Main(string[] args)
{
Collection<string> myitems = new Collection<string>();
myMthod(ref myitems);
Console.WriteLine(myitems.Count.ToString());
Console.ReadLine();
}
static void myMthod(ref Collection<string> myitems)
{
myitems.Add("string");
if(myitems.Count <5)
myMthod(ref myitems);
}
}
As Stated by @Zooba Passing by ref is not necessary here, if you passing by value it will also work.
正如@Zooba 所述,这里不需要通过 ref 传递,如果您通过值传递它也可以工作。
回答by Matthew M. Osborn
As far as merging goes:
至于合并:
I wonder, is there a more efficient way to have a recursive function that returns a list of strings without duplicates? I don't have to use a Collection, it can be pretty much any suitable data type.
我想知道,有没有更有效的方法来让递归函数返回一个没有重复的字符串列表?我不必使用集合,它几乎可以是任何合适的数据类型。
Your function assembles a return value, right? You're splitting the supplied list in half, invoking self again (twice) and then merging those results.
你的函数组合了一个返回值,对吗?您将提供的列表一分为二,再次调用 self(两次),然后合并这些结果。
During the merge step, why not just check before you add each string to the result? If it's already there, skip it.
在合并步骤中,为什么不在将每个字符串添加到结果之前进行检查?如果它已经存在,请跳过它。
Assuming you're working with sorted lists of course.
当然,假设您正在使用排序列表。