本節將講述如何查詢工做項,用於二次開發中定義獲取工做項列表。異步
使用WorkItemStore.Query方法進行查詢工做項,其使用的語法和SQL語法相似:學習
Select [標題]spa
from workitemscode
where [工做項類型]='任務' and [指派給] = 'administrator'blog
order by [標題]排序
咱們經過多個步驟來學習,1、咱們鏈接TFS服務:ip
//TFSURI Uri tfsUri = new Uri("http://pc-20130113jkun:8080/tfs"); TfsTeamProjectCollection projectCollection = new TfsTeamProjectCollection(tfsUri); WorkItemStore workItemStore = (WorkItemStore)projectCollection.GetService(typeof(WorkItemStore));
2、基本查詢開發
//基本查詢 WorkItemCollection queryResults = workItemStore.Query(@" Select [標題] From WorkItems Where [工做項類型] = 'Bug' "); foreach (WorkItem item in queryResults) { Console.WriteLine(" 工做項名稱:"+item.Title+" 工做項描述:"+item.Description); }
3、多條件查詢和排序get
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); }
4、查詢結果數量string
Console.WriteLine("--------------------------查詢結果數量-------------------------"); //查詢結果數量 string queryString = @" Select [標題] From WorkItems Where [工做項類型] = 'Bug'"; Query query = new Query(workItemStore,queryString); int numWorkItems = query.RunCountQuery(); Console.WriteLine("工做項數量 " + numWorkItems + " user stories.");
5、異步查詢
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();