基于C#中的两个属性对directorySearcher进行排序 [英] Sorting on directorySearcher based on two properties in c#

查看:70
本文介绍了基于C#中的两个属性对directorySearcher进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据部门,然后按名称(均按字母顺序)获取排序后的 SearchResultCollection 对象.我正在尝试加载两个属性,但这仅采用指定的最后一个属性并根据该属性对其进行排序.

I'm trying to get a sorted SearchResultCollection object based on department, and then by name (both alphabetical). I'm trying to load two properties, but this merely takes the last property specified and sorts it based on that.

我当前的代码如下:

DirectoryEntry entry = new DirectoryEntry(ConfigurationManager.AppSettings["LDAP"]);
DirectorySearcher search = new DirectorySearcher(entry)
{
    SearchScope = SearchScope.Subtree,
    Filter = "(&(objectClass=user)(physicalDeliveryOfficeName=Dartmouth))"
};
search.PropertiesToLoad.Add("name");
search.PropertiesToLoad.Add("phone");
search.PropertiesToLoad.Add("email");
search.PropertiesToLoad.Add("department");

search.Sort.Direction = System.DirectoryServices.SortDirection.Ascending;
search.Sort.PropertyName = "department";
search.Sort.PropertyName = "name";

SearchResultCollection result = search.FindAll(); 

但同样,这仅按名称排序.我需要所有按部门分组的用户,并从那里按名称排序.

But again, this only sorts by name. I need all users grouped by department, and from there sorted by name.

推荐答案

SearchResultCollection 转换为 IEnumerable ,然后使用自定义的 IComparer 您开发的

Cast the SearchResultCollection to an IEnumerable and then sort with a custom IComparer that you develop

var results = search.FindAll().Cast<SearchResult>()
                              .Sort(/*your IComparer<SearchResult>*/);

在客户端对结果进行排序,因为AD中的服务器端排序会占用大量资源.

Sort the results on client side because server side sorts in AD are resource intensive.

此外-如果您拥有AD 2000,则它不支持对多个属性的搜索

这篇关于基于C#中的两个属性对directorySearcher进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆