1

我创建了一个函数来使用 Microsoft Graph Education API 返回结果。我想打印反馈和积分,但我不能,它们不存在。这是代码:

public static async Task<IEnumerable<EducationOutcome>> GetOutcomesForSubmission()
{
  var outcomes = await graphClient
    .Education
    .Classes["8557483b-a233-4710-82de-e1bdb03bb9a9"]
    .Assignments["1b09cd43-cf87-4cef-a043-ae3d6160c200"]
    .Submissions["d4486e20-1b47-4b5b-720c-0fe0038d4882"]
    .Outcomes
    .Request()
    .GetAsync();

  return outcomes;
}

public static void ListOutcomes()
{
  var outcomes = GetOutcomesForSubmission().Result;

  Console.WriteLine("Outcomes:\n");

  foreach (var v in outcomes)
  {
    Console.WriteLine($"User id: {v.LastModifiedBy.User.Id}, Submission id: {v.Id}");
  }

  Console.WriteLine("\n");
}
4

1 回答 1

0

您的问题是GetAsync()没有返回EducationOutcome对象集合。它正在返回一个EducationSubmissionOutcomesCollectionPage。要获得实际结果,您需要返回CurrentPage属性。

public static async Task<IEnumerable<EducationOutcome>> GetOutcomesForSubmission()
{
  var response = await graphClient
    .Education
    .Classes["8557483b-a233-4710-82de-e1bdb03bb9a9"]
    .Assignments["1b09cd43-cf87-4cef-a043-ae3d6160c200"]
    .Submissions["d4486e20-1b47-4b5b-720c-0fe0038d4882"]
    .Outcomes
    .Request()
    .GetAsync();

  return response.CurrentPage.ToList();
}

请注意,这只会返回第一页数据。如果要获取所有数据,则需要使用response.NextPageRequest属性进行分页:

var outcomes = response.CurrentPage.ToList();
while (response.NextPageRequest != null)
{
    response = await response.NextPageRequest.GetAsync();
    outcomes.AddRange(response.CurrentPage);

}
return outcomes;

请记住,这EducationOutcome是一个基类,因此它只包含所有“结果”类型共有的属性(在这种情况下很少)。如果您想要特定类型的 Outcome,则需要先将其转换为特定类型:

foreach (var v in outcomes)
{
  if (v is EducationRubricOutcome)
  {
    var outcome = v as EducationRubricOutcome;
    Console.WriteLine($"User id: {outcome.LastModifiedBy.User.Id}, Submission id: {outcome.Id}, Feedback Count: {outcome.RubricQualityFeedback.Count}");
  }
  else if (v is EducationPointsOutcome)
  {
    var outcome = v as EducationPointsOutcome;
    Console.WriteLine($"User id: {outcome.LastModifiedBy.User.Id}, Submission id: {outcome.Id}, Points: {outcome.Points.Points}");
  }
  else if (v is EducationFeedbackOutcome)
  {
    var outcome = v as EducationFeedbackOutcome;
    Console.WriteLine($"User id: {outcome.LastModifiedBy.User.Id}, Submission id: {outcome.Id}, Feedback: {outcome.Feedback.Text}");
  }
}
于 2021-03-02T23:40:31.033 回答