public SearchResult GetSearchResult(IFilter filter, string[] propertiesToLoad)
        {
            SearchResult      searchResult;
            DirectorySearcher directorySearcher = null;

            if (propertiesToLoad == null)
            {
                directorySearcher = new DirectorySearcher(this.DirectoryEntry, filter.BuildFilter());
            }
            else
            {
                directorySearcher = new DirectorySearcher(this.DirectoryEntry, filter.BuildFilter(), propertiesToLoad);
            }
            using (directorySearcher)
            {
                if (this.QueryScopeType == QueryScopeType.OneLevel)
                {
                    directorySearcher.SearchScope = SearchScope.OneLevel;
                }
                else if (this.QueryScopeType == QueryScopeType.Subtree)
                {
                    directorySearcher.SearchScope = SearchScope.Subtree;
                }
                searchResult = directorySearcher.FindOne();
            }
            return(searchResult);
        }
        public SearchResultCollection GetSearchResultCollection(IFilter filter, string[] propertiesToLoad)
        {
            SearchResultCollection searchResultCollection;
            DirectorySearcher      directorySearcher = null;

            if (propertiesToLoad == null)
            {
                directorySearcher = new DirectorySearcher(this.DirectoryEntry, filter.BuildFilter());
            }
            else
            {
                directorySearcher = new DirectorySearcher(this.DirectoryEntry, filter.BuildFilter(), propertiesToLoad);
            }
            using (directorySearcher)
            {
                if (this.QueryScopeType == QueryScopeType.OneLevel)
                {
                    directorySearcher.SearchScope = SearchScope.OneLevel;
                }
                else if (this.QueryScopeType == QueryScopeType.Subtree)
                {
                    directorySearcher.SearchScope = SearchScope.Subtree;
                }
                directorySearcher.PageSize  = Int32.MaxValue;
                directorySearcher.SizeLimit = Int32.MaxValue;
                searchResultCollection      = directorySearcher.FindAll();
            }
            return(searchResultCollection);
        }
        /// <summary>
        /// Find all AD objects by filter.
        /// </summary>
        /// <param name="filter">The filter</param>
        /// <returns>All AD objects.</returns>
        public static List <TADObject> FindAllByFilter <TADObject>(IFilter filter, string[] propertiesToLoad = null, QueryScopeType queryScopeType = QueryScopeType.Subtree)
        {
            List <TADObject> adObjects;

            using (var searchResultCollection = GetSearchResultCollection(filter.BuildFilter(), propertiesToLoad, GetSearchScope(queryScopeType)))
            {
                var objects = (from SearchResult searchResult in searchResultCollection
                               select GetActiveDirectoryObject(searchResult)).ToList();
                adObjects = objects.Cast <TADObject>().ToList();
            }
            return(adObjects);
        }
        /// <summary>
        /// Find one AD object by filter.
        /// </summary>
        /// <param name="filter">The filter</param>
        /// <returns>One AD object.</returns>
        public static TADObject FindOneByFilter <TADObject>(IFilter filter, string[] propertiesToLoad = null, QueryScopeType queryScopeType = QueryScopeType.Subtree) where TADObject : ActiveDirectoryObject
        {
            TADObject    adObject = null;
            SearchResult searchResult;

            using (DirectorySearcher directorySearcher = new DirectorySearcher(ClientContext.GetContext().RootDirectoryEntry, filter.BuildFilter(), propertiesToLoad, GetSearchScope(queryScopeType)))
            {
                searchResult = directorySearcher.FindOne();
                if (searchResult != null)
                {
                    adObject = GetActiveDirectoryObject(searchResult) as TADObject;
                }
            }
            return(adObject);
        }