简体   繁体   English

从DirectorySearcher获取2000条记录中的2000条

[英]Get 2000 of 6000 records from DirectorySearcher

I have a code which either gets 1000 or all the results using DirectorySearcher . 我有一个代码,使用DirectorySearcher获得1000或所有结果。

But I want to get only 2000 of 6000 results. 但是我想要获得6000个结果中的2000个。

Here is the code to get 6000 results whereas I only want 2000 这是获得6000结果的代码,而我只想要2000

mySearcher.SizeLimit = 2000;
mySearcher.PageSize = 1000;

SearchResultCollection results = mySearcher.FindAll();

int totalUsers = results.Count;

Please help. 请帮忙。

Thanks 谢谢

My research says PageSize = 0 gives 1000 results only and Pagesize = 1000 gives all results. 我的研究表明,PageSize = 0仅提供1000个结果,Pagesize = 1000给出所有结果。

Need more help to make this work. 需要更多帮助才能完成这项工作。

There seems to be a slight weird behavior with the PageSize property. 使用PageSize属性似乎有一些奇怪的行为
Please set it to 0 , does that work? 请将其设置为0 ,这有用吗?

By the way: 顺便说说:
c# Active Directory Services findAll() returns only 1000 entries c #Active Directory服务findAll()仅返回1000个条目

Can I get more than 1000 records from a DirectorySearcher in Asp.Net? 我可以从Asp.Net中的DirectorySearcher获取超过1000条记录吗?

According to this post: DirectorySearcher.FindAll() - should have PageSize=1000 根据这篇文章: DirectorySearcher.FindAll() - 应该有PageSize = 1000

SizeLimit doesn't matter in this case as the Server side default is being used which defaults to 1000 results. 在这种情况下SizeLimit无关紧要因为正在使用服务器端默认值,默认为1000个结果。 I've never needed to page like this but I imagine the minimum size limit is used (between your size limit and server size limit - just tested it in my own AD). 我从来不需要这样的页面,但我想使用了最小大小限制(在您的大小限制和服务器大小限制之间 - 只在我自己的AD中测试它)。 Your PageSize is indeed paging however it does the paging in the background and returns you only the final stream as I understand which is why you are getting all results. 你的PageSize确实是分页,但是它在后台进行分页,只返回最后一个流,因为我理解这就是你得到所有结果的原因。

I believe your easiest solution is to just use Linq on top of this and do a .Take(2000) on results. 我相信你最简单的解决办法就是在这个问题上使用Linq,并在结果上做一个.Take(2000) This would get you your desired result at the cost of additional bandwidth and processing on server. 这将以您在服务器上的额外带宽和处理成本为您获得所需的结果。

If you really want to sort that, I guess you will have to go update the servers default paging size to be higher (though i doubt this will be feasible for administrative reasons). 如果你真的想要对它进行排序,我想你必须更新服务器默认的分页大小更高(尽管我怀疑这是出于管理原因这是可行的)。

EDIT: 编辑:

Here is how I'd go about this roughly (quick working sample code from my LinqPad - also note that I'm actually bringing over everything unlike how you might wanna do this by taking the for loop out of equation): 下面是我大致如何粗略地对待这个问题(来自我的LinqPad的快速工作示例代码 - 还注意到我实际上带来的一切都不像你想要通过取出等式的for循环来做到这一点):

using(DirectoryEntry de = new DirectoryEntry("LDAP://domain.local/dc=domain,dc=local", "user", "password"))
using(DirectorySearcher ds = new DirectorySearcher(de))
{
    ds.Filter="(&(objectCategory=user)(objectClass=user))";
    ds.PageSize= 1000;
    ds.PropertiesToLoad.Clear();
    ds.PropertiesToLoad.Add("objectGuid");

    var results = ds.FindAll();
    var searchResults = results.Cast<System.DirectoryServices.SearchResult>().ToArray();
    int myDesiredPageSize = 2000;

    var upns = new StringCollection();

    for(var step=0; step < Math.Ceiling((double)results.Count / myDesiredPageSize); step++)
    {
        Parallel.ForEach(searchResults.Skip(step*myDesiredPageSize).Take(myDesiredPageSize), result => {
        using(var entry = result.GetDirectoryEntry())
        {
            entry.RefreshCache(new[]{ "userPrincipalName" });

            if(entry.Properties.Contains("userPrincipalName"))
                upns.Add(entry.Properties["userPrincipalName"][0] as string);
        }
        });
    }

    upns.Count.Dump();
}

This returns about 1400 results in about 3 seconds over my lan connection in my test scenario. 在我的测试场景中,我的局域网连接在大约3秒内返回大约1400个结果。 Since we are querying in parallel higher numbers should not scale linearly. 由于我们并行查询,因此较高的数字不应线性扩展。 Also you may want to contain the parallelisation to some degree as this would hammer AD mercilessly :) 此外,您可能希望在某种程度上包含并行化,因为这将无情地锤击AD :)

In my normal operations, I employ caching with WhenChanged attribute of AD objects to reduce my actual querying to only changed objects instead of loading the same thing over and over which only takes the hit first time around and subsequent results are fraction of a second. 在我的正常操作中,我使用AD对象的WhenChanged属性进行缓存,以将我的实际查询减少到仅更改的对象,而不是一遍又一遍地加载相同的东西,这只是第一次触及,后续结果只有几分之一秒。 Taking this approach you can do away with paging entirely and just load your properties when you start and then pull only the changed entries if that is an option. 采用这种方法,您可以完全取消分页,只需在启动时加载属性,然后只选择已更改的条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以从 DirectorySearcher 中获取 1000 多条记录吗? - Can I get more than 1000 records from a DirectorySearcher? 从固定宽度的平面文件到SQL 2000中获取数百万条记录 - Get millions of records from fixed-width flat file to SQL 2000 使用C#从Active Directory中从DirectorySearcher获取子对象属性的最有效方法 - Most efficient way to get child object properties from a DirectorySearcher result in Active Directory using C# 从 Principal 而不是 DirectorySearcher 的 SearchResult 获取额外的 Active Directory 属性 - Get additional Active directory attributes from Principal instead of DirectorySearcher's SearchResult DirectorySearcher:如何仅获取“真实”用户的条目 - DirectorySearcher: How to get only entries for “real” users 如何在.NET中获取DirectorySearcher的SearchResult的DN? - How to get DN for a DirectorySearcher's SearchResult in .NET? "获取分页 DirectorySearcher 的搜索结果总数" - Get total number of search results for paginated DirectorySearcher Linq查询花费很长时间查询具有6000条记录的表 - Linq query taking a long time to query a table with 6000 records LINQ - 通过WHERE子句查询大约6000条唯一记录 - LINQ - Querying about 6000 unique records by WHERE clause 从SID获取非Windows 2000之前的组名 - Get Non Pre-Windows 2000 Group Name from SID
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM