DirectorySearch.PageSize = 2 doesn't work

前端 未结 2 1842
孤城傲影
孤城傲影 2021-01-19 05:31
     using (DirectorySearcher srch = new DirectorySearcher(String.Format(\"(memberOf=  {0})\",p_Target.DistinguishedName)))
     {
            srch.PageSize = 2;
            


        
相关标签:
2条回答
  • 2021-01-19 05:43

    Look at this code and related article by Ethan Wilansky,

    http://msdn.microsoft.com/en-us/magazine/dd250826.aspx?code=true&level=root&file=VLVSrch.cs

    Does exactly what you want.

    0 讨论(0)
  • 2021-01-19 05:54

    The pageSize is to set the number of records returned in one paged search. Paged search is an underlying thing at LDAP protocol level. It's transparent to you. Although you set the PageSize to 2, DirectorySearcher will return all the results for you but in your case in two paged search reply packets.

    To do what you want, you should use SizeLimit instead. It will control how many records returned in total.

    Here is one more tricky thing. Windows Server has a limit set on the server side. In each of the paged search result, it can only return at most 1000 entries. So, you need to be careful setting the PageSize and SizeLimit if you have the results more than 1000 entries. If you set PageSize=0 (meaning unlimited) and SizeLimit=0 (meaning unlimited), you will get an error because Windows server cannot return you more than 1000 entries in one single page. If you set Pagesize = 800 and SizeLimit=0 (meaning unlimited), you will get all your result and if you look at the network sniffer, you will see there are a bunch of LDAP paged search result. In each of the paged search result, you see 800 entries.

    EDIT

    Here is a more elabrated reply to the question in your comment.

    Hm, interesting. Please help me understand better this mechanism: if in AD I have 5000 rows, PageSize of DirectorySearcher is set at 1000, SizeLimit is set to 0 and max server limit is 1000. How many call of directorySearcher.FindAll() I need to have in my code to get all 5000 results? 5 or 1

    Regardless of how many number of records going to be returned, you always need only 1 call on DirectorySearcher. DirectorySearcher will handle the rest for you. It will aggregate the paged search result and present to you in one single IEnumerable even though the data might be from different reply packets. I guess you want to set PageLimit because you don't want all 5000 results returned all at once and occupy your memory. Don't worry on that. DirectorySearcher won't store all 5000 results in your memory as long as you don't hold reference on each of the returned SearchResult. It won't wait till all reply packets coming back either. As soon as the first reply packet coming back, the FindAll() returns the result to you. If your program is so fast that after you process the 1000 results, the second paged search result packet has still not yet arrived. The call on the MoveNext() will be blocked and wait till the second paged search result packet recevied.

    0 讨论(0)
提交回复
热议问题