private static T FindFirstMatchingObject <T>(QueryFilter filter, bool includeAccountForestRootOrg, bool includeResourceForest, bool useGC, ConsistencyMode consistencyMode, bool useRecipientSession, bool includeSoftDeletedObject, bool includeSecondaryPartitions) where T : ADObject, new()
        {
            IEnumerable <ADSessionSettings> enumerable = PartitionDataAggregator.CreateSessionSettingsCollection(includeAccountForestRootOrg, includeResourceForest, includeSecondaryPartitions);

            foreach (ADSessionSettings adsessionSettings in enumerable)
            {
                if (includeSoftDeletedObject)
                {
                    adsessionSettings.IncludeSoftDeletedObjects = true;
                }
                IDirectorySession directorySession = PartitionDataAggregator.GetDirectorySession(consistencyMode, useRecipientSession, adsessionSettings);
                directorySession.UseGlobalCatalog = useGC;
                T[] array = directorySession.Find <T>(null, QueryScope.SubTree, filter, null, 1);
                if (array != null && array.Length != 0)
                {
                    return(array[0]);
                }
            }
            return(default(T));
        }
        private static IEnumerable <T> FindPagedAllMatchingObjects <T>(QueryFilter filter, bool includeAccountForestRootOrg, bool includeResourceForest, bool useGC, ConsistencyMode consistencyMode, bool useRecipientSession, bool includeSoftDeletedObject, bool includeSecondaryPartitions) where T : ADObject, new()
        {
            IEnumerable <ADSessionSettings> sessionSettingsCollection = PartitionDataAggregator.CreateSessionSettingsCollection(includeAccountForestRootOrg, includeResourceForest, includeSecondaryPartitions);

            foreach (ADSessionSettings sessionSettings in sessionSettingsCollection)
            {
                if (includeSoftDeletedObject)
                {
                    sessionSettings.IncludeSoftDeletedObjects = true;
                }
                IDirectorySession session = PartitionDataAggregator.GetDirectorySession(consistencyMode, useRecipientSession, sessionSettings);
                session.UseGlobalCatalog = useGC;
                ADPagedReader <T> reader = session.FindPaged <T>(null, QueryScope.SubTree, filter, null, 0, null);
                foreach (T result in reader)
                {
                    yield return(result);
                }
            }
            yield break;
        }