Efficient way to retrieve work item details from a Linked Query using TFS API

Retrieve WorkItems details of a linked query using TFS API

Problem Statement:
We can query a linked Query item using TFS Workitem Query language, But the challenge with linked query is, it displays only
SourceId and TargetId along with LinkTypeId. It does not display other important fields like title, status, description e.t.c

In Order to display the same we will have
a. Get all Source and Target Id(Linked Work Item Id’s) using a Linked Query to a datastructure
b. Now iterate over each Id and make API call to get detail info for each item.
This involves too many calls to TFS server.

For Example: To Query bugs that have user stories. We need
a. Execute a linked query to get all Source and Target Id(Linked Work Item Id’s)
b. Execute a flat query to bug details(Bug title, status, Prority etc.) for each Target Id.
Lets say if the query has 10,000 bugs we will have to make 10,000 calls to TFS to get bug details for each target id.

We can write one single query passing all Target Ids and get workitem details in one go.

Below is Code snippet to retreive work item details using a Linked Query (i.e Bug details of bugs that has user stories)

// Linked query to display Source(Bug id) and their respectived TargetId(Linked user story work item id) for Project “My Project”
Query query = new Query(_store, string.Format(
“SELECT [System.Id] FROM WorkItemLinks¬† WHERE ([Source].[System.TeamProject] = ‘My Project’¬† AND¬† AND [Source].[System.WorkItemType] = ‘Bug’) And ([System.Links.LinkType] <> ”) And ([Target].[System.WorkItemType] = ‘User Story’) ORDER BY [System.Id] mode(MustContain)”));


// Get list of work item Id for which we want to retrieve more detailed information like bug title, status, description
int[] ids = (from WorkItemLinkInfo info in wlinks
select info.TargetId).ToArray();

// Use flat query to get detailed workitem info for list of workitem ids
var DetailedInfoQuery = new Query(_store, mydetails, ids);
WorkItemCollection workitems = DetailedInfoQuery .RunQuery();

foreach (WorkItem wi in workitems)
WorkItemType worktype = wi.Type;
string WorkItemType = worktype.Name;
string id = wi.Id;
string bugtile = wi.Title;