我可以得到从Asp.Net一个DirectorySearcher从1000多个记录? [英] Can I get more than 1000 records from a DirectorySearcher in Asp.Net?

查看:565
本文介绍了我可以得到从Asp.Net一个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(DirectorySearcher 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的价值上面,但你可以使用一个较小的值,如果preferred。代价是:用一个小每页会返回结果更快的每一页,但需要遍历大量结果的时更频繁调用服务器

  • 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.

在默认情况下搜索不分页(每页= 0)。在这种情况下最多的sizeLimit结果被返回。

by default the search isn't paged (PageSize = 0). In this case up to SizeLimit results is returned.

由于蔽日指出,重要的是要通过处置返回的FindAll的SearchResultCollection,否则在MSDN文档DirectorySearcher.FindAll的备注部分描述可能有内存泄漏:<一href="http://msdn.microsoft.com/en-us/library/system.directoryservices.directorysearcher.findall.aspx">http://msdn.microsoft.com/en-us/library/system.directoryservices.directorysearcher.findall.aspx

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: http://msdn.microsoft.com/en-us/library/system.directoryservices.directorysearcher.findall.aspx

,以帮助避免这种在.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
}

您可以再使用此如下:

using(DirectorySearcher srch = new DirectorySearcher(dirEnt, "(objectClass=Group)", loadProps))
{
    srch.PageSize = 1000;
    var results = SafeFindAll(srch);
}

这篇关于我可以得到从Asp.Net一个DirectorySearcher从1000多个记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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