【发布时间】:2011-01-22 10:32:09
【问题描述】:
我正在尝试编写一个实用程序来查看自我存储在数据库中的日期以来用户是否已登录到 Windows。
private void bwFindDates_DoWork(object sender, DoWorkEventArgs e)
{
UserPrincipal u = new UserPrincipal(context);
u.SamAccountName = "WebLogin*";
PrincipalSearcher ps = new PrincipalSearcher(u);
var result = ps.FindAll();
foreach (WebAccess.WebLoginUsersRow usr in webAccess.WebLoginUsers)
{
UserPrincipal b = (UserPrincipal)result.
Single((a) => a.SamAccountName == usr.WEBUSER);
if (b.LastLogon.HasValue)
{
if (b.LastLogon.Value < usr.MODIFYDATE)
usr.LastLogin = "Never";
else
usr.LastLogin = b.LastLogon.Value.ToShortDateString();
}
else
{
usr.LastLogin = "Never";
}
}
}
但是性能很慢。我从中提取的用户列表有大约 150 个 Windows 用户,所以当我点击UserPrincipal b = (UserPrincipal)result.Single((a) => a.SamAccountName == usr.CONVUSER); 时,每个用户需要 10 到 15 秒才能完成(单步执行我可以看到它正在执行步骤 a.SamAccountName == usr.CONVUSE 运行对于每个人,所以最坏的情况是运行 O(n^2) 次)
对提高效率的方法有什么建议吗?
【问题讨论】:
-
我讨厌 SO somtimes 喜欢处理代码部分中的选项卡,有时却不喜欢。
标签: c# linq optimization search