合并两个集合< T> [英] Merging two Collection<T>

查看:185
本文介绍了合并两个集合< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个返回收藏℃的功能;串> ,并递归调用自己最终能够返回一个大的收藏<串>

I got a Function that returns a Collection<string>, and that calls itself recursively to eventually return one big Collection<string>.

现在,我不知道什么是最好的方式来合并列表? Collection.CopyTo()只复制到字符串[],并使用的foreach()回路感觉就像是低效的。然而,因为我也想筛选出重复的,我觉得我会与调用包含一个foreach结束()收藏

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.

我不知道,有没有办法有一个返回字符串没有重复列表的递归函数更有效的方法?我没有使用收藏,也可以是几乎任何合适的数据类型。

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.

只有排除,我不能不Visual Studio 2005和.NET 3.0,所以没有LINQ

Only exclusion, I'm bound to Visual Studio 2005 and .net 3.0, so no LINQ.

修改:要澄清:该功能需要用户出的Active Directory,着眼于用户的直接下属,然后递归地看着每一个用户的直接报告。所以,最终的结果是,在这经常执行给定user.Since的指挥链,并在此刻需要20秒对一些用户的所有用户的名单,我正在寻找方法来改善它。缓存结果24小时,也是我的名单上顺便说一句,但我想看看如何应用缓存之前改进它。

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.

推荐答案

如果您正在使用目录<>你可以使用.AddRange一个列表添加到其他列表

If you're using List<> you can use .AddRange to add one list to the other list.

或者你可以使用收益返回列表上结合飞象这样:

Or you can use yield return to combine lists on the fly like this:

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;
}

这篇关于合并两个集合&LT; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆