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();
复制代码

  




本文转自程兴亮博客园博客,原文链接:http://www.cnblogs.com/chengxingliang/p/3251376.html,如需转载请自行联系原作者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值