如何使用Graph API打印反馈和积分?

How to print feedback and points using Graph API?

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

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");
}

您的问题是 GetAsync() 不是 returning EducationOutcome 对象的集合。它是 returning 一个 EducationSubmissionOutcomesCollectionPage 而不是。要获得实际结果,您需要 return 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();
}

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

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

}
return outcomes;

请记住,EducationOutcome 是一个 base class,因此它将只包含所有“结果”类型(在此案例很少)。如果您想要特定类型的结果,您需要先将其转换为特定类型:

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}");
  }
}