C# 微软图形。追踪页码

C# 微软图形。追踪页码,c#,microsoft-graph-api,C#,Microsoft Graph Api,我正在实现一种方法,通过对组的displayName属性进行分页和筛选,从AzureAD获取组。让我们把焦点放在传呼上。实际上,我可以用$top生成页面结果。但我的要求是,我需要从特定页面返回数据集。我也能够做到这一点,但没有一个非常优雅的解决方案,向结果的NextPageRequest对象发出请求以检索指定页面。代码如下: public async Task<List<Group>> GetGroups(string filterText, int page, int

我正在实现一种方法,通过对组的
displayName
属性进行分页和筛选,从AzureAD获取组。让我们把焦点放在传呼上。实际上,我可以用
$top
生成页面结果。但我的要求是,我需要从特定页面返回数据集。我也能够做到这一点,但没有一个非常优雅的解决方案,向结果的
NextPageRequest
对象发出请求以检索指定页面。代码如下:

public async Task<List<Group>> GetGroups(string filterText, int page, int pageSize = 1)
{
  var graphClient = _graphSdkHelper.GetAuthenticatedClient();
  List<Group> groups = new List<Group>();

  //Microsoft Graph api allows a minimum page size of 1 and maximum of 999
  if (pageSize < 1 || pageSize > 999)
  {
    return groups;
  }

  try
  {
    IGraphServiceGroupsCollectionPage graphGroups;
    if (!string.IsNullOrEmpty(filterText))
    {
       graphGroups = await graphClient.Groups.Request().Filter($"startswith(displayName, '{filterText}')").Top(pageSize).GetAsync();
    }
    else
    {
      //if filter text is empty, return all groups
      graphGroups = await graphClient.Groups.Request().OrderBy("displayName").Top(pageSize).GetAsync();
    }

    //navigate to the requested page. This is extremly inefficient as we make requests until we find the right page.
    //$Skip query parameter doesn't work in groups services
    var currentPage = 1;
    while (currentPage < page && graphGroups.NextPageRequest != null && (graphGroups = await graphGroups.NextPageRequest.GetAsync()).Count > 0)
    {
      currentPage = currentPage + 1;
    }

    foreach (var graphGroup in graphGroups)
    {
      Group group = _graphSdkHelper.TranslateGroup(graphGroup);
      groups.Add(group);
    }
  }
  catch (Exception exception)
  {
    _logger.LogError(exception, "Error while searching for groups");
  }

  return groups;
}
public异步任务GetGroups(字符串filterText,int-page,int-pageSize=1)
{
var graphClient=_graphsdkheloper.GetAuthenticatedClient();
列表组=新列表();
//Microsoft Graph api允许最小页面大小为1,最大为999
如果(页面大小<1 | |页面大小>999)
{
返回组;
}
尝试
{
IGraphServiceGroupsCollectionPage图形组;
如果(!string.IsNullOrEmpty(filterText))
{
graphGroups=wait graphClient.Groups.Request().Filter($“startswith(displayName,{filterText}'))).Top(pageSize.GetAsync();
}
其他的
{
//如果筛选文本为空,则返回所有组
graphGroups=等待graphClient.Groups.Request().OrderBy(“displayName”).Top(pageSize.GetAsync();
}
//导航到请求的页面。这是非常低效的,因为在找到正确的页面之前,我们会发出请求。
//$Skip查询参数在组服务中不起作用
var currentPage=1;
而(currentPage0)
{
currentPage=currentPage+1;
}
foreach(图组中的变量图组)
{
Group Group=_graphSdkHelper.TranslateGroup(graphGroup);
组。添加(组);
}
}
捕获(异常)
{
_logger.LogError(异常,“搜索组时出错”);
}
返回组;
}
问题1:我如何改进和跟踪我是哪一页?这可能吗?现在我可以请求下一个页面,所以如果我需要访问第20页,例如,意味着对azure的20个请求

关于AzureAD Graph Api的这篇文章,这是不可能的:

我只是拒绝认为在AzureAD做真正的分页是不可能的

问题2:如何通过一个请求获得组的总数。似乎没有为组或用户实现
$Count
查询参数:

谢谢