Ejemplo n.º 1
0
        private IEnumerable <T> GetAllEntitiesAsync(IOrganizationService service, QueryExpression qe, int?maxCount, int?pageSize)
        {
            var          page        = qe.PageInfo;
            IAsyncResult asyncResult = null;

            EntityRetrievingMethod = GetEntitiesWithCookie;
            int count = 0;

            if (maxCount != null && pageSize == null && maxCount < DEFAULT_PAGE_SIZE)
            {
                // Updte page Size to Max Count to limit the number of records retrieved
                pageSize = maxCount;
            }

            // Check for page Size / Max Count Settings
            if (maxCount < pageSize)
            {
                pageSize = maxCount;
            }

            if (pageSize != null && pageSize > 0)
            {
                page.Count = pageSize.Value;
            }

            page.PageNumber   = 1;
            page.PagingCookie = null;

            var response = GetEntitiesWithCookie(service, qe);

            while (response.MoreRecords && response.Entities != null && (maxCount == null || maxCount.Value <= count))
            {
                UpdatePageCount(page, ref count, maxCount);
                page.PageNumber++;
                page.PagingCookie = response.Cookie;

                // Perform Async call for next set, while yield returning current set
                try
                {
                    asyncResult = EntityRetrievingMethod.BeginInvoke(service, qe, null, this);

                    // Retrieve all records from the result set.
                    foreach (T entity in response.Entities)
                    {
                        yield return(entity);
                    }
                }
                finally
                {
                    if (asyncResult != null)
                    {
                        response = EntityRetrievingMethod.EndInvoke(asyncResult);
                        asyncResult.AsyncWaitHandle.Close();
                    }
                }
            }

            if (response.Entities == null)
            {
                yield break;
            }
            else
            {
                foreach (T entity in response.Entities)
                {
                    yield return(entity);
                }
            }
        }
        private IEnumerable <T> GetAllEntitiesInstance(IOrganizationService service, QueryExpression qe, int?maxCount, int?pageSize, bool async)
        {
            var          page        = qe.PageInfo;
            IAsyncResult asyncResult = null;

            EntityRetrievingMethod = GetEntitiesWithCookie;
            var count = 0;

            if (maxCount != null && pageSize == null && maxCount < DefaultPageSize)
            {
                // Update page Size to Max Count to limit the number of records retrieved
                pageSize = maxCount;
            }

            // Check for page Size / Max Count Settings
            if (maxCount < pageSize)
            {
                pageSize = maxCount;
            }

            if (pageSize != null && pageSize > 0)
            {
                page.Count = pageSize.Value;
            }

            page.PageNumber   = 1;
            page.PagingCookie = null;

            var response = GetEntitiesWithCookie(service, qe);

            while (response.MoreRecords && response.Entities != null && (maxCount == null || maxCount.Value <= count))
            {
                UpdatePageCount(page, ref count, maxCount);
                page.PageNumber++;
                page.PagingCookie = response.Cookie;

                // Perform Async call for next set, while yield returning current set
                try
                {
                    // If async, begin request to get new entities before yielding results
                    if (async)
                    {
                        asyncResult = EntityRetrievingMethod.BeginInvoke(service, qe, null, this);
                    }
                    // Retrieve all records from the result set.
                    foreach (var entity in response.Entities)
                    {
                        yield return(entity);
                    }

                    // If sync, wait until all entities have been returned, then get entities
                    if (!async)
                    {
                        response = GetEntitiesWithCookie(service, qe);
                    }
                }
                finally
                {
                    if (asyncResult != null)
                    {
                        response = EntityRetrievingMethod.EndInvoke(asyncResult);
                        asyncResult.AsyncWaitHandle.Close();
                    }
                }
            }

            if (response.Entities == null)
            {
                yield break;
            }

            foreach (var entity in response.Entities)
            {
                yield return(entity);
            }
        }