简体   繁体   English

基于C#中的两个属性对directorySearcher进行排序

[英]Sorting on directorySearcher based on two properties in c#

I'm trying to get a sorted SearchResultCollection object based on department, and then by name (both alphabetical). 我试图获取基于部门的排序SearchResultCollection对象,然后按名称(按字母顺序)。 I'm trying to load two properties, but this merely takes the last property specified and sorts it based on that. 我正在尝试加载两个属性,但这仅采用指定的最后一个属性并基于该属性对其进行排序。

My current code is the following: 我当前的代码如下:

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. 我需要所有按部门分组的用户,并从那里按名称排序。

Cast the SearchResultCollection to an IEnumerable and then sort with a custom IComparer that you develop SearchResultCollection强制转换为IEnumerable ,然后使用您开发的自定义IComparer进行排序

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

Sort the results on client side because server side sorts in AD are resource intensive. 在客户端对结果进行排序,因为AD中的服务器端排序会占用大量资源。

In addition - if you have AD 2000 it doesn't support search on more than one attribute 另外-如果您有AD 2000,则它不支持对多个属性的搜索

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM