WIQL 查询不包括 "System.AssignedTo" 字段

WIQL Query not including "System.AssignedTo" Field

我有这个 WIQL,它的目的是找到分配给工作项的用户。

var wiql = string.Format("SELECT [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State]" +
                           " FROM WorkItems" +
                           " WHERE ([System.TeamProject] = '{0}')" +
                           " AND ([System.WorkItemType] = 'Task' OR [System.WorkItemType] = 'Bug')" + 
                           " ORDER BY [System.Id]", projectName);

我是这样执行的...

Wiql wiql = new Wiql() { Query = wiqlQueryString };

  using (var workItemTrackingHttpClient = new WorkItemTrackingHttpClient(VstsAccess.AccessUri, VstsAccess.AccessCredentials))
  {
    var workItemQueryResult = workItemTrackingHttpClient.QueryByWiqlAsync(wiql).Result;

    if (workItemQueryResult != null && workItemQueryResult.WorkItemRelations.Any())
    {
      List<int> sourceIdList = new List<int>();
      foreach (var item in workItemQueryResult.WorkItemRelations)
        sourceIdList.Add(item.Target.Id);

      int[] arr = sourceIdList.ToArray();
      string[] fields = { "System.Id", "System.WorkItemType", "System.AssignedTo", "System.Title", "System.Description", "System.State", "System.IterationPath", "System.TeamProject", "System.ChangedDate", "System.ChangedBy", "System.CreatedDate" };
      return workItemTrackingHttpClient.GetWorkItemsAsync(arr, fields, workItemQueryResult.AsOf).Result;
    }
    else
      return new List<WorkItem>();
  }

但是 "AssignedTo" 和 "Description" 字段没有出现在工作项的字典字段集中。为什么会这样,我该如何解决?

您可以使用下面的代码查询工作项,它有 "AssignedTo" 和 "Description" 字段值。

WorkItemStore workItemStore = teamProjectCollection.GetService<WorkItemStore>();
string queryString = string.Format("SELECT [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State]" +
                           " FROM WorkItems" +
                           " WHERE ([System.TeamProject] = '{0}')" +
                           " AND ([System.WorkItemType] = 'Task' OR [System.WorkItemType] = 'Bug')" +
                           " ORDER BY [System.Id]", "Mtt-Scrum"); ;

// Create and run the query.
Query query = new Query(workItemStore, queryString);
WorkItemCollection witCollection = query.RunQuery();

foreach (Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem workItem in witCollection)
{
    ......
}

查询结果将只包含非空字段,即没有人分配给工作项,该字段根本不会在 Fields 字典中。

您需要对这些字段实施自定义检查并根据您的逻辑将它们分配给某些东西:

            int[] arr = ids.ToArray();

            string[] fields = new string[] {
                "System.Id", 
                "System.Title",
                "System.State",
                "System.AssignedTo"
            };

            var workItems = await workItemTrackingHttpClient.GetWorkItemsAsync(arr, fields, workItemQueryResult.AsOf);

            List<WorkItemData> list = new List<WorkItemData>();

            foreach (var workItem in workItems)
            {
                var wi = new WorkItemData(workItem.Id.Value);

                wi.Title = workItem.Fields["System.Title"].ToString();
                wi.State = workItem.Fields["System.State"].ToString();
                wi.AssignedTo = workItem.Fields.ContainsKey("System.AssignedTo") ? workItem.Fields["System.AssignedTo"].ToString() : "";

                list.Add(wi);

            }