简体   繁体   English

DirectorySearcher SizeLimit和List.Sort产生不必要的结果C#

[英]DirectorySearcher SizeLimit and List.Sort producing unwanted results C#

I have some code which takes an input string of users to search for in Active Directory. 我有一些代码,需要使用用户输入字符串在Active Directory中进行搜索。 I limit the number of results (to 20) returned for searches. 我限制了搜索返回的结果数(最多20个)。 I then sort the results by the Active Directory created date descending. 然后,我按Active Directory创建的日期降序对结果进行排序。 However, if I have more than 20 results (eg 100) on a particular user (eg 'Smith'), I get 20 users (due to my limitiing of the results) sorted by created date, but it's the last 20 of the 100 sorted by created date, where I want the first 20. If I remove the SizeLimit, I get all 100 results in the correctly sorted order. 但是,如果我在一个特定用户(例如“ Smith”)上有20个以上的结果(例如100个),则我会获得20个用户(由于对结果的限制),按创建日期排序,但这是100个中的最后20个按创建日期排序,我需要前20个。如果删除SizeLimit,则将以正确的排序顺序获得所有100个结果。 Below is my code, not sure what needs to be adjusted. 下面是我的代码,不确定需要调整什么。

        public void getADSearchResults(string searchString)
        {

            //Create list to hold ADUser objects
            List<ADUser> users = new List<ADUser>();
            string[] allUsers = searchString.Split(new Char[] { ',' }, userSearchLimit);

            var json = "";

            foreach (string name in allUsers)
            {
                //Connect to the root of the active directory domain that this computer is bound to with the default credentials; seems to cover employee and provider OUs at minimum
                var rootDirectory = new DirectoryEntry();

                DirectorySearcher adSearch = new DirectorySearcher(rootDirectory);

                adSearch.Filter = "(&(objectClass=user)(anr=" + name + "))";

                //LIMIT NUMBER OF RESULTS PER USER SEARCHED
                adSearch.SizeLimit = 20;

                // Go through all entries from the active directory.
                foreach (SearchResult adUser in adSearch.FindAll())
                {
                    DirectoryEntry de = adUser.GetDirectoryEntry();

                    string userName = "";
                    userName = adUser.Properties["sAMAccountName"][0].ToString();

                    string createdDate = "";
                    createdDate = adUser.Properties["whenCreated"][0].ToString();

                    ADUser aduser = new ADUser(userName, createdDate);

                    users.Add(aduser);
                }
            }

            users.Sort((x, y) => DateTime.Parse(y.createdDate).CompareTo(DateTime.Parse(x.createdDate)));

            json = new JavaScriptSerializer().Serialize(new { users = users });

            //return json;
            HttpContext.Current.Response.Write(json);
        }

public class ADUser
{
    public ADUser(string UserName, string CreatedDate)
    {
        userName = UserName;
        createdDate = CreatedDate;
...
}

I suppose you have a logical mistake there. 我想你在那里有一个逻辑上的错误。

I suppose you need to cut this operation in two pieces: 我想您需要将此操作分为两部分:

1) Get the whole files and users. 1)获取整个文件和用户。 2) Make a loop to critic if an user has appeared before and, if so, jump all his records until the next user. 2)如果有用户之前出现过,请给评论者循环,如果是,请跳过其所有记录,直到下一位用户为止。

The problem may be related to the fact you want to perform it in a single operation AND cutting only the 20 first/last records. 该问题可能与您要在单个操作中执行并且仅剪切20个第一条/最后一条记录的事实有关。

Good luck 祝好运

PS: can you put a USER FILTER there? PS:您可以在其中放置一个用户过滤器吗? I mean, after has the whole files in a LIST filter it by user? 我的意思是,列表中的整个文件经过用户筛选之后?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM