我可以从 DirectorySearcher 中获取 1000 多条记录吗? [英] Can I get more than 1000 records from a DirectorySearcher?

查看:20
本文介绍了我可以从 DirectorySearcher 中获取 1000 多条记录吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚注意到结果的返回列表限制为 1000.我的域(巨大域)中有 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 所指出的,处理 FindAll 返回的 SearchResultCollection 很重要,否则可能会出现内存泄漏 如 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆