本节将讲述如何查询工做项,用于二次开发中定义获取工做项列表。异步
使用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();