如何通过1查询在c#中通过Wiql获取任务ID(storyid,Featureid,Feature名称)? [英] how to get a Task id (storyid,Featureid,Feature name) by Wiql in c# by 1 Query)?
问题描述
我具有这样的层次结构,其中一个功能可以具有许多用户故事,每个用户故事具有一对多的任务.
但是,用户故事可能没有父功能,而某些任务可能没有父用户故事
i have a Hierarchical structure like this in which a Feature can have many User Stories each having one to many Tasks.
But a User Story may have no parent Feature as well as some Task may have no parent User Story
F1
-->U1,U2,U3
--->t1,t2,t3
我需要c#中的Wiql查询,通过该查询,对于任何Taskid输入,我都会获得其父Storyid(如果没有父ID,则为0)以及后一个父Feature ID(如果没有父User Story,则为0)和名称(如果功能ID为0,则为其他")
I need a Wiql Query in c# by which for any Taskid input I get its parent storyid (or 0 if it has no parent) along with this latter parent Feature id (0 if it has no parent User Story) and name ('other' if Feature id is 0)
推荐答案
通过此查询,您可以找到工作项的任何父分支:
You may find any parent branch for work item with this query:
您可以使用以下代码获取父母名单:
You can use this code to get a list of parents:
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;
}
}
}
您还可以修改上一个周期并获取父信息(用户故事或功能)
Also you can modify the last cycle and get parent info (user story or feature)
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);
这篇关于如何通过1查询在c#中通过Wiql获取任务ID(storyid,Featureid,Feature名称)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!