2012-04-14 86 views
3

分選的DirectorySearcher查詢的結果我有以下代碼:通過日期時間

// 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

創建比較器,通過他們的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()); 
+0

一般情況下,我要堅持像AD上面的一個作爲服務器端各種各樣的解決方案都是資源密集型和故障易髮結果集較大。 – 2012-04-15 20:09:26

+0

謝謝你的迴應,我認爲這會奏效。我會看看我能做些什麼。 – Dbloom 2012-04-16 16:59:24

+0

我最終創建了一個爲我保存所有AD屬性的類,然後創建了一個SortedList 。這使我可以自動按日期排列列表中的元素,並且反轉順序非常簡單:'IEnumerable > revList = oldCompsList.Reverse();' – Dbloom 2012-04-24 21:19:06

0

可以如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 \ Parameters
  • 添加字符串值「DSA Heuristics」
  • 將該值設置爲000000000001
  • 重啓
  • 後,這個問題解決了