我可以从DirectorySearcher获得1000条以上的记录吗?

我只是注意到结果的返回列表限制为1000。我的域(HUGE域)中有1000多个组。如何获得超过1000条记录?我可以从以后的唱片开始吗?我可以将其划分为多个搜索吗?


这是我的查询:


DirectoryEntry dirEnt = new DirectoryEntry("LDAP://dhuba1kwtn004");

string[] loadProps = new string[] { "cn", "samaccountname", "name", "distinguishedname" };

DirectorySearcher srch = new DirectorySearcher(dirEnt, "(objectClass=Group)", loadProps);

var results = srch.FindAll();

我试图设置srch.SizeLimit = 2000; ,但这似乎不起作用。有任何想法吗?


摇曳的蔷薇
浏览 671回答 1
1回答

忽然笑

您需要将DirectorySearcher.PageSize设置为非零值以获取所有结果。顺便说一句,您还应该在处理完DirectorySearcher后再对其进行处置using(var srch = new DirectorySearcher(dirEnt, "(objectClass=Group)", loadProps)){&nbsp; &nbsp; srch.PageSize = 1000;&nbsp; &nbsp; var results = srch.FindAll();}API文档不是很清楚,但是本质上是:当您进行分页搜索时,将忽略SizeLimit,并且在您遍历FindAll返回的结果时将返回所有匹配的结果。结果将一次从服务器检索到一页。我选择了1000以上的值,但是如果愿意,可以使用较小的值。折衷方案是:使用较小的PageSize将更快地返回结果的每一页,但在迭代大量结果时将需要更频繁地调用服务器。默认情况下,不分页搜索(PageSize = 0)。在这种情况下,最多返回SizeLimit结果。正如Biri所指出的,处置由FindAll返回的SearchResultCollection很重要,否则,可能会发生内存泄漏,如DirectorySearcher.FindAll的MSDN文档的备注部分所述。在.NET 2.0或更高版本中,避免这种情况的一种方法是编写一个自动处理SearchResultCollection的包装器方法。这可能看起来像以下内容(或可能是.NET 3.5中的扩展方法):public IEnumerable<SearchResult> SafeFindAll(DirectorySearcher searcher){&nbsp; &nbsp; using(SearchResultCollection results = searcher.FindAll())&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; foreach (SearchResult result in results)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return result;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; } // SearchResultCollection will be disposed here}然后,您可以按以下方式使用它:using(var srch = new DirectorySearcher(dirEnt, "(objectClass=Group)", loadProps)){&nbsp; &nbsp; srch.PageSize = 1000;&nbsp; &nbsp; var results = SafeFindAll(srch);}
打开App,查看更多内容
随时随地看视频慕课网APP