我有以下代码:
// 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/)
对此进行排序的最佳方式是什么?
答案 0 :(得分:1)
您可以按照MSDN here中提到的那样执行服务器端:
new DirectorySearcher(entry)
{
Sort = new SortOption("cn", SortDirection.Ascending),
PropertiesToLoad = {"cn"}
};
已解决链接的问题主题:
我们在AD Windows 2008 R2上遇到了同样的问题
- 已应用kb977180-v2 http://support.microsoft.com/kb/977180
- 并添加了密钥 HKLM \系统\ CurrentControlSet \服务\ NTDS \参数
- 添加字符串值“DSA Heuristics”
- 将值设置为000000000001
- 重新开始
- 此问题解决后
答案 1 :(得分:0)
创建比较器,用于比较SearchResult实例的whenCreated属性
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());