我有一些代码,用于在Active Directory中搜索用户的输入字符串。我限制为搜索返回的结果数(到20)。然后,我按Active Directory创建的日期降序对结果进行排序。但是,如果我对某个特定用户有超过20个结果(例如100个)(例如'史密斯'),我会根据创建的日期排序20个用户(由于我对结果的限制),但它&# 39; s按创建日期排序的100个中的最后20个,我想要前20个。如果我删除SizeLimit,我会按正确排序的顺序获得所有100个结果。以下是我的代码,不确定需要调整的内容。
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;
...
}
答案 0 :(得分:0)
我想你在那里有一个逻辑错误。
我想你需要将这个操作分成两部分:
1)获取整个文件和用户。 2)如果用户之前已经出现过,请向批评者循环,如果是,则将所有记录跳转到下一个用户。
问题可能与您想要在单个操作中执行它并且仅剪切20个第一个/最后一个记录的事实有关。
祝你好运PS:你可以在那里放一个用户过滤器吗?我的意思是,在LIST中的所有文件都由用户过滤后?