如何通过 1 个查询在 c# 中通过 Wiql 获取任务 ID(storyid、Featureid、功能名称)?

how to get a Task id (storyid,Featureid,Feature name) by Wiql in c# by 1 Query)?

我有一个像这样的层次结构,其中一个功能可以有许多用户故事,每个用户故事都有一对多的任务。
但是用户故事可能没有父特征,某些任务可能没有父用户故事

F1
  -->U1,U2,U3 
     --->t1,t2,t3

我需要一个 c# 中的 Wiql 查询,对于任何 Taskid 输入,我都可以获得其父故事 ID(如果没有父故事,则为 0)以及后一个父特征 ID(如果没有父用户故事,则为 0)和名称('other' 如果功能 ID 为 0)

您可以通过此查询找到工作项的任何 parent 分支:

您可以使用此代码获取 parent 的列表:

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System;
using System.Collections.Generic;
using System.Linq;

namespace QueryLinkedWIQL
{
    class Program
    {
        static void Main(string[] args)
        {
            int _id = 742;

            try
            {
                TfsTeamProjectCollection _tpc = new TfsTeamProjectCollection(new Uri("http://myserver/DefaultCollection"));

                WorkItemStore _wistore = _tpc.GetService<WorkItemStore>();

                var _parents = GetParentsWithWIQL(_wistore, _id);

                if (_parents.Count == 0)
                {
                    Console.WriteLine("There is no parent for ID: " + _id);
                    return;
                }

                for (int i = 0; i < _parents.Count; i++)
                    Console.WriteLine("{0} parent: Team project '{1}', Type '{2}', Id '{3}', Title '{4}'",
                        (i == 0) ? "Main" : "Next", _parents[i].Project.Name, _parents[i].Type.Name, _parents[i].Id, _parents[i].Title);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
        }

        private static List<WorkItem> GetParentsWithWIQL(WorkItemStore pWiStore, int pId)
        {
            List<WorkItem> _parents = new List<WorkItem>();

            string _wiql = String.Format("SELECT [System.Id] FROM WorkItemLinks WHERE ([Source].[System.WorkItemType] <> '') And ([System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward') And ([Target].[System.Id] = {0}) ORDER BY [System.Id] mode(Recursive,ReturnMatchingChildren)", pId);

            Query _query = new Query(pWiStore, _wiql);

            WorkItemLinkInfo[] _links = _query.RunLinkQuery();

            for (int i = 0; i < _links.Count(); i++)
                if (_links[i].TargetId != pId) _parents.Add(pWiStore.GetWorkItem(_links[i].TargetId));

            return _parents;
        }
    }
}

您还可以修改上一个周期并获取 parent 信息(用户故事或功能)

            if (_parents.Count == 0)
            {
                Console.WriteLine("There is no parent for ID: " + _id);
                return;
            }

            int _featureId = 0, _userstoryId = 0;
            string _featureTitle = "", _userstoryTitle = "";

            for (int i = 0; i < _parents.Count; i++)
                switch (_parents[i].Type.Name)
                {
                    case "Feature":
                        if (_featureId == 0) { _featureId = _parents[i].Id; _featureTitle = _parents[i].Title; }
                        break;
                    case "Product Backlog Item": //for scrum process template. for agile use User Story
                        if (_userstoryId == 0) { _userstoryId = _parents[i].Id; _userstoryTitle = _parents[i].Title; }
                        break;
                }

            if (_featureId != 0) Console.WriteLine("The main parent is Feature: {0}: '{1}'", _featureId, _featureTitle);
            else if (_userstoryId != 0) Console.WriteLine("The main parent is User Story: {0}: '{1}'", _userstoryId, _userstoryTitle);