.Net MVC5 C#中的Active Directory用户字典上的自动完成linq#

时间:2015-10-14 16:17:24

标签: c# linq active-directory asp.net-mvc-5 iqueryable

我正试图让我的自动完整存储库方法(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();
    }

0 个答案:

没有答案