我正试图让我的自动完整存储库方法(linq)使用我刚刚创建的活动目录字典,但我似乎无法理解它...
// My ADUsers Dictionary
public Dictionary<string, string> ADUsers()
{
List<KeyValuePair<string, string>> ADUsersList = new List<KeyValuePair<string, string>>();
using (var context = new PrincipalContext(ContextType.Domain, "creaghconcrete.com"))
{
using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
{
foreach (var result in searcher.FindAll())
{
DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
string samAccName = de.Properties["samAccountName"].Value.ToString();
string realName = string.Format("{0} {1}", de.Properties["givenName"].Value, de.Properties["sn"].Value);
ADUsersList.Insert(0, new KeyValuePair<string, string>(samAccName, realName));
}
}
}
var dictionary = ADUsersList.ToDictionary((keyItem) => keyItem.Key, (valueItem) => valueItem.Value);
return dictionary;
}
// My GetAutoADUsers method
public List<Schema.Models.ADUser> GetAutoADUsers(string query)
{
IQueryable<Schema.Models.ADUser> result = ADUsers().Where(x => x.Key != null && x.Value != null);
if (string.IsNullOrEmpty(query)) return result.OrderBy(u => u.RealName).ToList();
string q = query.ToLower();
return result.Where(u => u.RealName.ToLower().Contains(q)
|| u.ADName.ToLower().Contains(q)
).OrderBy(u => u.RealName).ToList();
}