2016-08-16 46 views
0

我試圖通過使用wiql查詢來獲取團隊項目的所有不同迭代路徑。c#WIQL查詢獲取所有不同的迭代路徑

我實際的解決方案如下:

我用這個查詢

public static readonly string IterationPathsQuery = @"SELECT [System.IterationPath] FROM workitems 
     WHERE[System.WorkItemType] = 'Requirement' 
     OR[System.WorkItemType] = 'Feature'"; 

要獲得所有相關的工作項和遍歷它們把所有的不同迭代路徑。

private void FillIterationPathComboBox(WorkItemStore wiStore) 
{ 
    WorkItemCollection wiCollection = wiStore.Query(Constants.IterationPathsQuery); 
    var paths = new List<string>(); 

    ... 
    foreach (WorkItem wi in wiCollection) 
    { 
     ... 

     if (!String.IsNullOrEmpty(wi.IterationPath) && !paths.Contains(wi.IterationPath)) 
     { 
      paths.Add(wi.IterationPath); 
     } 
    } 

    foreach (string path in paths) 
    { 
     IterationPathComboBox.Items.Add(path); 
    } 
} 

但是這個解決方案並沒有很好的性能。 有沒有辦法只查詢使用的不同迭代路徑?我已經讀過「不同」並不支持,但也許有一種方法我還沒有考慮。

+0

你想獲得該項目的一些具體工作項目的所有迭代路徑或只是不同的迭代路徑? –

回答

0

WIQL查詢無法過濾不同的迭代路徑。這裏有兩種方案:

  1. 您可以將查詢導出到Excel並使用Excel RemoveDuplicates方法來篩選不同的迭代路徑。

  2. 您可以獲取迭代路徑列表,然後使用LINQ刪除重複項和獲取不同記錄。檢查this website上的代碼片段。

    using System; 
    using System.Collections.Generic; 
    using System.Data; 
    using System.Linq; 
    
    namespace AbundantCode 
    { 
        internal class Program 
        { 
         //How to Remove Duplicates and Get Distinct records from List using LINQ ? 
    
         private static void Main(string[] args) 
         { 
          List<Employee> employees = new List<Employee>() 
    { 
    
    new Employee { EmpID = 1 , Name ="AC"}, 
    new Employee { EmpID = 2 , Name ="Peter"}, 
    new Employee { EmpID = 3 , Name ="Michael"}, 
    new Employee { EmpID = 3 , Name ="Michael"} 
    }; 
    
    //Gets the Distinct List 
    var DistinctItems = employees.GroupBy(x => x.EmpID).Select(y => y.First()); 
          foreach (var item in DistinctItems) 
          Console.WriteLine(item.Name); 
          Console.ReadLine(); 
         } 
        } 
    
        public class Employee 
        { 
         public string Name { get; set; } 
         public int EmpID { get; set; } 
        } 
    } 
    
+0

謝謝! 替代方案2顯着提高了我方法的性能。 – Goldi