如果我这样做:
RelatedLink newLink = new RelatedLink(linkTypeEnd, id);
if (workItem.Links.Contains(newLink)) return;
workItem.Links.Add(newLink);
它仍然在 Add 方法上崩溃并出现 ValidationException,表明该链接已在集合中。
TF237099: Duplicate work item link.
那么 Contains 真正检查的是什么?引用相等?肯定不是吗?
有人知道如何处理这个问题吗?我正在编写一个工具来将需求从众所周知的工具迁移到 TFS。
最佳答案
因此,您有一个给定的 WorkItem(假设 ID = 1000)并且您想向其中添加一个相关的 WorkItem(假设 ID = 1001)。
只是为了
workItem.Links.Add(newLink);
不起作用,因为如果 WI 1001 已经是 WI 1000 的链接,它会抛出您提供的异常。
所以在添加之前我们需要检查WI 1001是否已经在1000的链接中。 这是可能的,如下所示:
WorkItem workItem = workItemStore.GetWorkItem(1000);
LinkCollection links = workItem.Links;
List<int> relatedWorkItemIds = new List<int>();
foreach (var link in links)
{
relatedWorkItemIds.Add(((Microsoft.TeamFoundation.WorkItemTracking.Client.RelatedLink) (link)).RelatedWorkItemId);
}
if(relatedWorkItemIds.Contains(1001))
{
return;
}
else
{
WorkItemLinkTypeEnd linkTypeEnd = workItemStore.WorkItemLinkTypes.LinkTypeEnds["Child"];
RelatedLink newLink = new RelatedLink(linkTypeEnd, 1001);
workItem.Links.Add(newLink);
}
努力回答我意识到你直接问“WorkItem.Links.Contains() 它有什么作用?” --> 我没有答案。
我希望您能以某种方式利用我上面实现的内容。
关于c# - WorkItem.Links.Contains() 它有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7838802/