Question

How to convert the below 2 foreach nested loops to LINQ query? It takes around 10 seconds to execute this loop which has around 610 items. Also, Is there any update to the lines of code below can be changed, so that the performance and can be improved a little and execution time can be reduced?

Code:

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;
    foreach (var testViewMap in testInViews)
    {
        if (!string.IsNullOrEmpty(TestLinkType))
        {
            var testLinkName = TestLinkType;
            var testChildType = testViewMap.ChildType;

            ITestLinkItem testItm = Factory.BuildLinkItem(testLinkName,testWorkbenchItem,testWorkbenchItem);
            lock (TestAddparents)
            {
                TestAddparents.Add(testItm);
            }
            break;
        }
    }
}
Était-ce utile?

La solution

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:

  1. you have an if condition within your loop which never changes if (!string.IsNullOrEmpty(TestLinkType)) => take it out
  2. 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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top