dezox
dezox

Reputation: 179

Is it possible to add SearchResultCollections together? Or return more than one?

Im searching in ActiveDirectory, and I have to make things perform fast. Sadly, I need to search in different domains and different OU-s. My goal is to write a function that does multiple searches, and returns the result of all search. Sadly, SearchResultCollections cannot be added together. I think converting them to any other IEnumerable would affect the speed, or iterating over the results building up a list from them would also be ineffective in terms of speed (Correct me here, If I'm wrong). Do you guys have any idea?

using System.DirectoryServices;

public SearchResultCollection SearchAD(ObjectClassValue objectClass, PropertyComparator comparator, string input, IEnumerable<string> searchPaths, IEnumerable<string> propertiesToCompareWith, IEnumerable<string> propertiesToLoad = null)
        {
            string objClass = getObjectClass(objectClass);
            DirectoryEntry entry;
            SearchResultCollection result = null;

            var searcher = new DirectorySearcher();
            searcher.PropertiesToLoad.AddRange(propertiesToLoad.ToArray());
            searcher.SearchScope = SearchScope.Subtree;
            searcher.Filter = buildSearchFilter(objectClass, comparator, input, propertiesToCompareWith);

            for (int i = 0; i < searchPaths.ToArray().Length; i++)
            {
                entry = new DirectoryEntry(searchPaths.ToArray()[i]);

                using (searcher)
                {
                    //here I want to add the results together
                }
            }
        }

Upvotes: 0

Views: 241

Answers (1)

Gabriel Luci
Gabriel Luci

Reputation: 40988

The only thing that stands out is your repeated use of searchPaths.ToArray(). You're converting that collection to an array twice on every iteration of the loop (once when it makes the comparison, and again when you use a value from it). It's an easy thing to change: Don't convert it to an array at all. Just use a foreach loop on searchPaths directly:

foreach (var searchPath in searchPaths)
{
    entry = new DirectoryEntry(searchPath);

    // etc
}

When programming with AD, the biggest slow downs will be from the number of network requests made and the amount of data you're asking for. I wrote an article about how to optimize performance, which might help you: Active Directory: Better Performance

You're already using PropertiesToLoad, which is good. That's important, for reasons I describe in that article. But there might be other things you can change in your code where you actually make the search.

Your query could also affect the speed of the search. A bad query can really slow things down. So be aware of that.

A small thing, which won't affect performance, Subtree is the default search scope, so this line isn't needed:

searcher.SearchScope = SearchScope.Subtree;

When it comes to combining the results, adding them all to the same collection in whatever format you need will be just fine. It won't add to the overall time in any significant way.

Upvotes: 1

Related Questions