To answer the actual question (according to the comments) - how to improve the performance, it is quite impossible if we don't know the reason for the code.
But there are some things i've noticed:
- you have an if condition within your loop which never changes
if (!string.IsNullOrEmpty(TestLinkType))
=> take it out - you run through the loop only once, so why having a loop in the first place, just get the first item.
The new code could look like this:
foreach(var map in infoMap)
{
var testItem = map.TestItem;
var testInViews = TestviewMaps;
var testWorkbenchItem = map.TestWorkbenchItem;
string TestLinkType = string.Empty;
WorkItemLinkCollection testLinkedWorkItems = testItem.WorkItemLinkHistory;
if (linkedWorkItems != null && linkedWorkItems.Count > 0)
TestLinkType = linkedWorkItems[0].LinkTypeEnd.LinkType.ReferenceName;
else if (testItem != null)
TestLinkType = testItem.Store.WorkItemLinkTypes.LinkTypeEnds["Parent"].LinkType.ReferenceName;
if (!string.IsNullOrEmpty(TestLinkType))
{
var testViewMap = testInViews.FirstOrDefault();
if (testViewMap != null)
{
var testLinkName = TestLinkType;
var testChildType = testViewMap.ChildType;
ITestLinkItem testItm = Factory.BuildLinkItem(testLinkName,testWorkbenchItem,testWorkbenchItem);
lock (TestAddparents)
{
TestAddparents.Add(testItm);
}
}
}
}
Now the actual question is, where is the time lost, how long does it take to get the value from the getters, are you doing any database calls or calls to external processes which take long? In that case you should retrieve as much data as possible at once and cache it.