哪种类型的收藏最好?

时间:2022-02-03 01:56:50

Wording in my previous question prevented people from answering it. Regardless to say, I can do what I need to do. My ultimate concern is which option is optimal?

我之前的问题中的措辞阻止了人们回答它。无论如何,我可以做我需要做的事情。我最关心的是哪个选项是最优的?

I am traversing the entire Active Directory for the domain and filtering it down by people/users.

我正在遍历域的整个Active Directory并由人/用户对其进行过滤。

I then need to throw this information into a collection.

然后我需要将这些信息放入一个集合中。

I know I can do it with

我知道我可以做到

List<Users>  // Create a model with the information i need and populate it.
Dictionary<string, List<Users>> // add a list of users to the string/department
Dictionary<string, Users> // struct or class
Lookup<string, Users>

I'm more looking for the optimal way, as going the Active Directory can be slow by it self I'm looking to optimize my code in other areas.

我更希望找到最佳方式,因为我自己想要在其他方面优化我的代码,因此Active Directory可能很慢。

I only need the department stored a single time, and each department can have many different users.

我只需要一次存储部门,每个部门可以有很多不同的用户。

What is the most optimal method to choose in this instance?

在这种情况下,最佳选择方法是什么?

Edit Addition:

编辑添加:

The only reason I need this specific code is for a specific page which will eventually allow me to loop through the contents and build out a drop down list. This code will not be used any where else.

我需要这个特定代码的唯一原因是一个特定的页面,它最终允许我遍历内容并构建一个下拉列表。此代码不会在任何其他地方使用。

1 个解决方案

#1


2  

A List is going to be faster when writing or accessing by index or scanning.

通过索引或扫描进行写入或访问时,列表会更快。

A Dictionary is going to be faster when accessing an item by its key (O(1) instead of O(N) or O(log N) in the list).

当通过键(O(1)而不是列表中的O(N)或O(log N))访问项目时,字典会更快。

If you are trying to add all users to a drop down box go with List<User>.

如果您尝试将所有用户添加到下拉框,请使用List

If you are going to add all departments to a drop down box and add the users from that department to a different drop down use Dictionary<Department, List<User>> and maybe do some profiling to see if it makes sense to have a separate list of departments.

如果要将所有部门添加到下拉框并将该部门的用户添加到不同的下拉列表,请使用Dictionary >并可能进行一些分析以查看是否有必要单独使用部门清单。 ,list>

If you are going to recreate the collection every time the page is accessed you will need to do some profiling with realish data to see which performs better.

如果您要在每次访问页面时重新创建集合,您将需要使用真实数据进行一些分析,以查看哪些性能更​​好。

Edit: Based on your update how about something like

编辑:根据您的更新如何做类似的事情

class Department
{
    public string Name {get;set;}
    public List<User> Users {get;set;}
}

List<Department> departments;

foo()
{
   foreach(department in departments)
   {
       // emit optgroup
       foreach(user in department.Users)
       {
           // emit option
       }
       // emit /optgroup
   }
}

or better yet cache the resulting HTML.

或者更好的是缓存生成的HTML。

#1


2  

A List is going to be faster when writing or accessing by index or scanning.

通过索引或扫描进行写入或访问时,列表会更快。

A Dictionary is going to be faster when accessing an item by its key (O(1) instead of O(N) or O(log N) in the list).

当通过键(O(1)而不是列表中的O(N)或O(log N))访问项目时,字典会更快。

If you are trying to add all users to a drop down box go with List<User>.

如果您尝试将所有用户添加到下拉框,请使用List

If you are going to add all departments to a drop down box and add the users from that department to a different drop down use Dictionary<Department, List<User>> and maybe do some profiling to see if it makes sense to have a separate list of departments.

如果要将所有部门添加到下拉框并将该部门的用户添加到不同的下拉列表,请使用Dictionary >并可能进行一些分析以查看是否有必要单独使用部门清单。 ,list>

If you are going to recreate the collection every time the page is accessed you will need to do some profiling with realish data to see which performs better.

如果您要在每次访问页面时重新创建集合,您将需要使用真实数据进行一些分析,以查看哪些性能更​​好。

Edit: Based on your update how about something like

编辑:根据您的更新如何做类似的事情

class Department
{
    public string Name {get;set;}
    public List<User> Users {get;set;}
}

List<Department> departments;

foo()
{
   foreach(department in departments)
   {
       // emit optgroup
       foreach(user in department.Users)
       {
           // emit option
       }
       // emit /optgroup
   }
}

or better yet cache the resulting HTML.

或者更好的是缓存生成的HTML。