TFS二次開發系列:五、工作項查詢


  本節將講述如何查詢工作項,用於二次開發中定義獲取工作項列表。

  使用WorkItemStore.Query方法進行查詢工作項,其使用的語法和SQL語法類似:

Select [標題]

from workitems

where [工作項類型]='任務' and [指派給] = 'administrator'

order by [標題]

  我們通過多個步驟來學習,一、我們連接TFS服務:

            //TFSURI
            Uri tfsUri = new Uri("http://pc-20130113jkun:8080/tfs");
            TfsTeamProjectCollection projectCollection = new TfsTeamProjectCollection(tfsUri);
            WorkItemStore workItemStore = (WorkItemStore)projectCollection.GetService(typeof(WorkItemStore));

  二、基本查詢

            //基本查詢
            WorkItemCollection queryResults = workItemStore.Query(@"
                Select  [標題] 
                From WorkItems
                Where [工作項類型] = 'Bug' ");
            foreach (WorkItem item in queryResults)
            {
                Console.WriteLine(" 工作項名稱:"+item.Title+" 工作項描述:"+item.Description);
            }

  三、多條件查詢和排序

           Console.WriteLine("--------------------------多條件查詢和排序-------------------------");
            //多條件查詢和排序
            WorkItemCollection itemcollection = workItemStore.Query(@"Select [標題] from workitems 
                where [工作項類型]='任務' and [指派給] = 'administrator' order by [標題] ");
            foreach (WorkItem item in itemcollection)
            {
                Console.WriteLine(" 工作項名稱:" + item.Title + " 工作項描述:" + item.Description);
            }

  四、查詢結果數量

            Console.WriteLine("--------------------------查詢結果數量-------------------------");
            //查詢結果數量
            string queryString = @" Select  [標題] From WorkItems Where [工作項類型] = 'Bug'";
            Query query = new Query(workItemStore,queryString); 
            int numWorkItems = query.RunCountQuery();
            Console.WriteLine("工作項數量 " + numWorkItems + " user stories.");

  五、異步查詢

            Console.WriteLine("--------------------------異步查詢-------------------------");
            //異步查詢
            ICancelableAsyncResult callback = query.BeginQuery();
            callback.AsyncWaitHandle.WaitOne(50, false);
            WorkItemCollection result = query.EndQuery(callback);
            foreach (WorkItem item in result)
            {
                Console.WriteLine(" 工作項名稱:" + item.Title + " 工作項描述:" + item.Description);
            }

  所有本文的代碼皆在下面。

            //TFSURI
            Uri tfsUri = new Uri("http://pc-20130113jkun:8080/tfs");
            TfsTeamProjectCollection projectCollection = new TfsTeamProjectCollection(tfsUri);
            WorkItemStore workItemStore = (WorkItemStore)projectCollection.GetService(typeof(WorkItemStore));
            Console.WriteLine("--------------------------基本查詢-------------------------");
            //基本查詢
            WorkItemCollection queryResults = workItemStore.Query(@"
                Select  [標題] 
                From WorkItems
                Where [工作項類型] = 'Bug' ");
            foreach (WorkItem item in queryResults)
            {
                Console.WriteLine(" 工作項名稱:"+item.Title+" 工作項描述:"+item.Description);
            }

            Console.WriteLine("--------------------------多條件查詢和排序-------------------------");
            //多條件查詢和排序
            WorkItemCollection itemcollection = workItemStore.Query(@"Select [標題] from workitems 
                where [工作項類型]='任務' and [指派給] = 'administrator' order by [標題] ");
            foreach (WorkItem item in itemcollection)
            {
                Console.WriteLine(" 工作項名稱:" + item.Title + " 工作項描述:" + item.Description);
            }

            Console.WriteLine("--------------------------查詢結果數量-------------------------");
            //查詢結果數量
            string queryString = @" Select  [標題] From WorkItems Where [工作項類型] = 'Bug'";
            Query query = new Query(workItemStore,queryString); 
            int numWorkItems = query.RunCountQuery();
            Console.WriteLine("工作項數量 " + numWorkItems + " user stories.");

            Console.WriteLine("--------------------------異步查詢-------------------------");
            //異步查詢
            ICancelableAsyncResult callback = query.BeginQuery();
            callback.AsyncWaitHandle.WaitOne(50, false);
            WorkItemCollection result = query.EndQuery(callback);
            foreach (WorkItem item in result)
            {
                Console.WriteLine(" 工作項名稱:" + item.Title + " 工作項描述:" + item.Description);
            }

            Console.ReadLine();

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM