3

我有以下代码:

// Declare new DirectoryEntry and DirectorySearcher
DirectoryEntry domainRoot = new DirectoryEntry("LDAP://rootDSE");
string rootOfDomain = domainRoot.Properties["rootDomainNamingContext"].Value.ToString();
DirectorySearcher dsSearch = new DirectorySearcher(rootOfDomain);

// Set the properties of the DirectorySearcher
dsSearch.Filter = "(objectClass=Computer)";
dsSearch.PropertiesToLoad.Add("whenCreated");
dsSearch.PropertiesToLoad.Add("description");
dsSearch.PropertiesToLoad.Add("operatingSystem");
dsSearch.PropertiesToLoad.Add("name");

// Execute the search
SearchResultCollection computersFound = dsSearch.FindAll();

我想按whenCreated属性按降序对结果进行排序,以便最新的计算机对象位于顶部。

我不能简单地做:

SortOption sortedResults = new SortOption("whenCreated", SortDirection.Descending);
dsSearch.Sort = sortedResults;

因为服务器返回错误(http://social.technet.microsoft.com/Forums/en-US/winserverDS/thread/183a8f2c-0cf7-4081-9110-4cf41b91dcbf/)

对此进行排序的最佳方法是什么?

4

2 回答 2

1

您可以按照 MSDN 中的说明在服务器端进行操作

      new DirectorySearcher(entry)
      {
        Sort = new SortOption("cn", SortDirection.Ascending),
        PropertiesToLoad = {"cn"}
      };

链接的问题线程已解决:

我们在 AD Windows 2008 R2 上遇到了同样的问题

  • 应用 kb977180-v2 http://support.microsoft.com/kb/977180
  • 并添加了密钥 HKLM\System\CurrentControlSet\Services\NTDS\Parameters
  • 添加字符串值“DSA 启发式”</li>
  • 将值设置为 000000000001
  • 重新开始
  • 这个问题解决后
于 2016-10-23T11:58:31.320 回答
0

创建比较器,通过 whenCreated 属性比较 SearchResult 实例

public class SearchResultComparer : Comparer<SearchResult>
{
    public override int Compare(SearchResult x, SearchResult y)
    {
        //Compare two SearchResult instances by their whenCreated property
    }
}

然后将所有项目复制到列表中,这将使用此比较器为您排序项目:

List<SearchResult> SearchResultList = new List<SearchResult>(computersFound);
SearchResultList.Sort(new SearchResultComparer());
于 2012-04-15T00:08:20.040 回答