Dynamic 分页查询

 

//前端
//单次查询的个数
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(true);
//按照时间进行排序
qe.Orders.Add(new OrderExpression("createdon", OrderType.Descending));
//分页查询
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);//判断查询是否还有数据
原文地址:https://www.cnblogs.com/LanHai12/p/15257938.html