分页查询
在使用CRM组织服务查询的时候,默认查询数据条数最大值为5000条,想查询全部的数据需要使用分页查询。
FetchXML查询
//单次查询的个数
int pageSize = 5000;
//第几页
int pageIndex = 1;
var ectemp= new EntityCollection();//临时的实体集合
var ec = new EntityCollection();//查询数据的实体集合
do
{
var fetchxml = string.Format(@"<fetch version='1.0' page='{0}' count='{1}' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='实体名称'>
<attribute name='查询的字段名' />
<attribute name='查询的字段名' />
<order attribute='排序字段' descending='false' />
</entity>
</fetch>", pageIndex, pageSize);
ectemp= CRMService.RetrieveMultiple(new FetchExpression(fetchxml));
if (ectemp?.Entities?.Count > 0)
{
foreach (var entity in ectemp.Entities)
{
ec.Entities.Add(entity);
}
pageIndex++;//页数++
}
else
{
break;//如果第一次没查到数据直接跳出
}
} while (ectemp!= null && ectemp.MoreRecords);//判断查询是否还有数据
表达式查询
//单次查询的个数
int pageSize = 5000;
//第几页
int pageIndex = 1;
var ec = new EntityCollection();//查询数据的实体集合
QueryExpression qe = new QueryExpression("实体名称");
qe.ColumnSet = new ColumnSet(new string[] { "查询字段名称" });
qe.PageInfo = new PagingInfo()
{
Count = pageSize,
PageNumber = pageIndex
};
do
{
ec= CRMService.RetrieveMultiple(qe);
if (ec?.Entities?.Count > 0)
{
qe.PageInfo.PageNumber++;//页数++
qe.PageInfo.PageCookie = ec.PagingCookie;//页数++
}
else
{
break;//如果第一次没查到数据直接跳出
}
} while (ec!= null && ec.MoreRecords);//判断查询是否还有数据