[英]How do I page my Active Directory query results?
我正在为我的组织开发基于Web的员工电话目录,该目录使用C#/ AccountManagement向Active Directory查询其数据。 我正在尝试查询AD,并根据返回的用户主体创建“雇员”列表。 我可以获取所有记录,但出于性能原因希望从服务器返回分页结果。 我试图在我的FindAll()方法上使用LINQ(Take and Skip),但是我遇到的问题是我进一步过滤结果以检查空的GivenName和Surname以摆脱测试帐户等。最终返回示例16当我尝试使用.Take(20)时的结果。 这是我在应用程序中使用AD的首次尝试,希望有人可以向我指出正确的方向,喜欢任何建议或批评。 下面是数据访问方法:
public static List<Employee> GetAllEmployees()
{
List<Employee> employees = new List<Employee>();
try
{
PrincipalContext context = new PrincipalContext(ContextType.Domain, "My Domain");
GroupPrincipal gp = GroupPrincipal.FindByIdentity(context, "Domain Users");
UserPrincipal principal = new UserPrincipal(context);
principal.Enabled = true;
using (PrincipalSearcher searcher = new PrincipalSearcher(principal))
{
searcher.QueryFilter = principal;
var results = searcher.FindAll();
foreach (UserPrincipal p in results)
{
if (p.IsMemberOf(gp))
{
if (p.GivenName != null && p.Surname != null)
{
string division = string.Empty;
DirectoryEntry de = p.GetUnderlyingObject() as DirectoryEntry;
if (de.Properties.Contains("department"))
{
division = de.Properties["department"].Value.ToString();
}
var employee = new Employee(p.GivenName, p.Surname, p.DisplayName, p.EmailAddress, division);
employees.Add(employee);
};
}
}
}
}
catch (Exception)
{
throw;
}
return employees;
}
原来答案是我忽略了的简单事情。 我将过滤移到了foreach循环之外。 我摆脱了:
if (p.IsMemberOf(gp))
和
if (p.GivenName != null && p.Surname != null)
并替换为:
principal.GivenName = "*";
principal.IsMemberOf(gp);
在我循环之前。 现在,我可以在searcher.Findall()
方法上执行.Take()
并收到我期望的快速结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.