我可以从DirectorySearcher获得1000条以上的记录吗? [英] Can I get more than 1000 records from a DirectorySearcher?
问题描述
我刚刚注意到结果的返回列表限制为1000。我的域(HUGE域)中有1000多个组。如何获得超过1000条记录?我可以从以后的唱片开始吗?我可以将其划分为多个搜索吗?
I just noticed that the return list for results is limited to 1000. I have more than 1000 groups in my domain (HUGE domain). How can I get more than 1000 records? Can I start at a later record? Can I cut it up into multiple searches?
这是我的查询:
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; ,但是似乎不起作用。有任何想法吗?
I have tried to set srch.SizeLimit = 2000;, but that doesn't seem to work. Any ideas?
推荐答案
您需要将DirectorySearcher.PageSize设置为非零值才能获得所有结果。
You need to set DirectorySearcher.PageSize to a non-zero value to get all results.
顺便说一句,您还应该在完成后使用DirectorySearcher
BTW you should also dispose DirectorySearcher when you're finished with it
using(var srch = new DirectorySearcher(dirEnt, "(objectClass=Group)", loadProps))
{
srch.PageSize = 1000;
var results = srch.FindAll();
}
API文档不是很清楚,但本质上是:
The API documentation isn't very clear, but essentially:
-
当您进行分页搜索时,将忽略SizeLimit,并且当您遍历FindAll返回的结果时,将返回所有匹配的结果。结果将一次从服务器检索到一页。我选择了1000以上的值,但是如果愿意,可以使用较小的值。折衷方案是:使用较小的PageSize将更快地返回结果的每一页,但是在迭代大量结果时将需要更频繁地调用服务器。
when you do a paged search, the SizeLimit is ignored, and all matching results are returned as you iterate through the results returned by FindAll. Results will be retrieved from the server a page at a time. I chose the value of 1000 above, but you can use a smaller value if preferred. The tradeoff is: using a small PageSize will return each page of results faster, but will require more frequent calls to the server when iterating over a large number of results.
默认情况下,不分页搜索(PageSize = 0)。在这种情况下,最多返回SizeLimit结果。
by default the search isn't paged (PageSize = 0). In this case up to SizeLimit results is returned.
正如Biri所指出的那样,重要的是要处置由返回的SearchResultCollection。 FindAll,否则您可能会出现所述的内存泄漏在DirectorySearcher.FindAll 的MSDN文档的备注部分中。
As Biri pointed out, it's important to dispose the SearchResultCollection returned by FindAll, otherwise you may have a memory leak as described in the Remarks section of the MSDN documentation for DirectorySearcher.FindAll.
在.NET 2.0或更高版本中避免这种情况的一种方法是编写包装方法会自动处理SearchResultCollection。这可能看起来像以下内容(或可能是.NET 3.5中的扩展方法):
One way to help avoid this in .NET 2.0 or later is to write a wrapper method that automatically disposes the SearchResultCollection. This might look something like the following (or could be an extension method in .NET 3.5):
public IEnumerable<SearchResult> SafeFindAll(DirectorySearcher searcher)
{
using(SearchResultCollection results = searcher.FindAll())
{
foreach (SearchResult result in results)
{
yield return result;
}
} // SearchResultCollection will be disposed here
}
然后可以按以下方式使用它:
You could then use this as follows:
using(var srch = new DirectorySearcher(dirEnt, "(objectClass=Group)", loadProps))
{
srch.PageSize = 1000;
var results = SafeFindAll(srch);
}
这篇关于我可以从DirectorySearcher获得1000条以上的记录吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!