2011-04-16 46 views
2

在查詢我們的AD時,我們會在初始查詢(samaccounttype=805306368)上獲得完整結果集。如果我們嘗試對值爲<not set>(例如givenname)的字段嘗試對查詢進行排序,則只會返回設置了值的記錄,並跳過沒有值的記錄。LDAP DirectorySearcher.sort缺少未設置值的記錄

如果
(|(&(samaccounttype=805306368)(givenName=*))(&(samaccounttype=805306368)(!givenname=*))) 

不知道它是關係到如何VirtualListView正在處理的結果集,或者如果有另一個問題:我們使用下面的存在標誌,包括兩套,但排序時還是鬆不設定值嘗試。有沒有人跑到這以前和任何建議我失蹤?這裏是ds.virtuallistview排序的代碼片段。

using (var ds = new DirectorySearcher(de)) 
      { 
       ds.Filter = Filter; 

       foreach (var p in Properties) 
       { 
        ds.PropertiesToLoad.Add(p.LDAPName); 
       } 

       //get record count 
       ds.PropertyNamesOnly = false; 
       ds.Sort = new SortOption(this.Properties.PrimaryOrderBy.LDAPName, SortDirection.Ascending); //ldap must always return ascending so we can custom sort 
       ds.VirtualListView = new DirectoryVirtualListView(0, 0, 0); 
       foreach(SearchResult s in ds.FindAll()){ /*must enumerate the collection before calling approximate total*/ } 
       Records = ds.VirtualListView.ApproximateTotal; 
       results = ds.FindAll(); 
       //Records = results.Count; 
      } 

回答

0

這裏是的DirectorySearcher的示例代碼上givenName該排序的結果。

static void Main(string[] args) 
{ 
    /* Connection to Active Directory 
    */ 
    DirectoryEntry deBase = new DirectoryEntry("LDAP://WM2008R2ENT:389/dc=dom,dc=fr"); 

    /* Directory Search 
    */ 
    DirectorySearcher dsLookFor = new DirectorySearcher(deBase); 
    dsLookFor.Filter = "(sn=users)"; 
    dsLookFor.SearchScope = SearchScope.Subtree; 
    dsLookFor.PropertiesToLoad.Add("cn"); 
    dsLookFor.PropertiesToLoad.Add("givenName"); 
    dsLookFor.PropertiesToLoad.Add("telephoneNumber"); 

    dsLookFor.Sort = new SortOption("givenName", SortDirection.Descending); 

    SearchResultCollection srcUsers = dsLookFor.FindAll(); 

    foreach (SearchResult sruser in srcUsers) 
    { 
    Console.WriteLine("{0}", sruser.Path); 

    foreach (string property in sruser.Properties.PropertyNames) 
    { 
     Console.WriteLine("\t{0} : {1} ", property, sruser.Properties[property][0]);   
    } 
    } 
} 

您可以在下面的結果看到,givenName沒有一套爲user1 users但對象在列表的末尾出現在結果中。我不使用VirtualListView

LDAP://WM2008R2ENT:389/CN=user0 users,OU=MonOu,DC=dom,DC=fr 
     givenname : user0 
     telephonenumber : 88 
     adspath : LDAP://WM2008R2ENT:389/CN=user0 users,OU=MonOu,DC=dom,DC=fr 
     cn : user0 users 
LDAP://WM2008R2ENT:389/CN=user2 users,OU=MonOu,DC=dom,DC=fr 
     givenname : user2 
     adspath : LDAP://WM2008R2ENT:389/CN=user2 users,OU=MonOu,DC=dom,DC=fr 
     cn : user2 users 
LDAP://WM2008R2ENT:389/CN=user1 users,OU=MonOu,DC=dom,DC=fr 
     telephonenumber : 99 
     adspath : LDAP://WM2008R2ENT:389/CN=user1 users,OU=MonOu,DC=dom,DC=fr 
     cn : user1 users 

如果我添加下列行:

dsLookFor.VirtualListView =新DirectoryVirtualListView(1,0,2);

結果是:

LDAP://WM2008R2ENT:389/CN=user0 users,OU=MonOu,DC=dom,DC=fr 
     givenname : user0 
     telephonenumber : 88 
     adspath : LDAP://WM2008R2ENT:389/CN=user0 users,OU=MonOu,DC=dom,DC=fr 
     cn : user0 users 
LDAP://WM2008R2ENT:389/CN=user2 users,OU=MonOu,DC=dom,DC=fr 
     givenname : user2 
     telephonenumber : 55 
     adspath : LDAP://WM2008R2ENT:389/CN=user2 users,OU=MonOu,DC=dom,DC=fr 
     cn : user2 users 

給內部消除名稱缺失的結果。