在 WIQL TFS 中如何知道一个 ID 是否有父 ID?

How to know if a ID has a parent ID or not in WIQL TFS?

我有一个 ID,我想通过 TFS 的 C# 编码知道是否有一个 parentid。在我的 TFS 看板中,有许多不包含 Feature ?

的用户故事

我在TFS中的层级结构如下:

Feature
--->User Stories(u1,u2,u3)
--->Tasks (t1,t2,t3)

有时用户故事不包含 Feature

您可以通过直接工作项查询来获取它。您可以在 VS 中创建它并保存到本地驱动器:

然后您可能会在保存的查询中找到查询文本:

<?xml version="1.0" encoding="utf-8"?><WorkItemQuery Version="1"><TeamFoundationServer>http://myserverandcollection</TeamFoundationServer><TeamProject>MyProject</TeamProject><Wiql>
SELECT [System.Id], [System.Links.LinkType], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State], [System.Tags] FROM WorkItemLinks WHERE ([Source].[System.Id] = 174) And ([System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Reverse') And ([Target].[System.WorkItemType] = 'Feature') ORDER BY [System.Id] mode(MustContain)
</Wiql></WorkItemQuery>

然后您可以使用 Microsoft.TeamFoundationServer.ExtendedClient nugate 包

创建应用程序
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QueryLinkedWIQL
{
    class Program
    {
        static void Main(string[] args)
        {
            WorkItemStore _wistore = new WorkItemStore("http://myserver/myCollection");

            int _id = 175;
            string _wiql = String.Format("SELECT [System.Id] FROM WorkItemLinks WHERE ([Source].[System.Id] = {0}) And ([System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Reverse') And ([Target].[System.WorkItemType] = 'Feature') ORDER BY [System.Id] mode(MustContain)", _id);

            Query _query = new Query(_wistore, _wiql);

            WorkItemLinkInfo[] _links = _query.RunLinkQuery();

            if (_links.Count() == 2) //only 1 child and its parent
                Console.WriteLine("Parent ID: " + _links[1].TargetId);
            else
                Console.WriteLine("There is no parent for ID: " + _id);
        }
    }
}

===========================for Relation Task->Something->Feature========= ===

您可以使用树查询:

和此代码:

using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QueryLinkedWIQL
{
    class Program
    {
        static void Main(string[] args)
        {
            WorkItemStore _wistore = new WorkItemStore("http://myserver/myCollection");

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

            Query _query = new Query(_wistore, _wiql);

            WorkItemLinkInfo[] _links = _query.RunLinkQuery();

            if (_links.Count() > 1) //first item contains feature
                Console.WriteLine("Parent ID: " + _links[0].TargetId);
            else
                Console.WriteLine("There is no parent for ID: " + _id);
        }
    }
}

------------屏幕数

查询:

调试: