大家应该知道Dynamic CRM 365 的组织服务查询,默认是最多5000笔的,虽然说可以去掉这个限制,但是去掉对系统性能会有影响,所以这里不建议大家去掉这个限制。
这里分享一种利用分页来解决这个限制的代码,是否使用就要看大家的业务需要了
private DataCollection<Entity> GetNoticeReaderList() { DataCollection<Entity> results = null; var pageIndex = 1; var pageSize = 5000; var queryExpression = new QueryExpression("product"); queryExpression.ColumnSet = new ColumnSet("productid", "name"); queryExpression.AddOrder("createdon", OrderType.Ascending); queryExpression.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0); queryExpression.PageInfo = new PagingInfo { PageNumber = pageIndex, Count = pageSize, PagingCookie = null }; while (true) { var ec = OrganizationService.RetrieveMultiple(queryExpression); if (ec == null || ec.Entities == null) { break; } if (results == null) { results = ec.Entities; } else { results.AddRange(ec.Entities); } if (!ec.MoreRecords) { break; } queryExpression.PageInfo.PageNumber++; queryExpression.PageInfo.PagingCookie = ec.PagingCookie; } return results; }