LDAP SearchResultCollection到Dictionary?

时间:2015-08-27 15:38:58

标签: c# linq dictionary directorysearcher

我获得了一个实现的接口,它有一个方法SearchUser(字符串过滤器)。在方法中,我需要搜索LDAP服务器并通过给定的过滤器获取用户。现在,我正在使用foreach循环。使用LINQ有更优雅的方法吗?

public List<Dictionary<string, object>> SearchUser2(string filter)
{
Dictionary<string, object> _SearchUser = null;
List<Dictionary<string, object>> _SearchList = new List<Dictionary<string, object>>();
DirectoryEntry domainEntry = new DirectoryEntry("LDAP://MyDomain");
DirectorySearcher searcher = new DirectorySearcher(domainEntry);
searcher.Filter = string.Format("(&(objectCategory=person)(objectClass=user)(mail={0}*))", filter);
DirectoryEntry entry;
SearchResultCollection searchResult = searcher.FindAll();
foreach (SearchResult sr in searchResult)
{
    entry = sr.GetDirectoryEntry();

    if (entry != null)
    {
        _SearchUser.Add("Mail", entry.Properties["Mail"].Value);
        .....
        _SearchList.Add(_SearchUser);
    }
}
return _SearchList;
}

0 个答案:

没有答案