Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a string in which i am using String.format in this i want to remove the 2nd parameter if string iteration will come as ="" or NULL and also want to remove the
. AND [Source].[System.IterationPath] IN ('{1}') from the String that will be formed.
is there any way besided the String format ?

What I have tried:

 public void GetProjectInfo(string projectname,string Iteration)
            {
    string querystring = string.Format("select [System.Id], [System.Title],[Story.Author],[Story.Owner],[System.AssignedTo]," +
" [System.WorkItemType],[Microsoft.VSTS.Scheduling.StoryPoints],[Microsoft.VSTS.Common.Priority]," +
"[Microsoft.VSTS.Scheduling.Effort], [Actual.Effort.Completed]" +
",[System.State]," +
 "[System.IterationPath]" +
 " FROM WorkItemLinks" +
 " WHERE" +
" ([Source].[System.TeamProject]='{0}'" +
 " and [Source].[System.WorkitemType] IN ('Feature', 'Bug', 'Product Backlog Item', 'Task')" +
    " AND [Source].[System.IterationPath] IN ('{1}'))"//this line of code will be removed along with iteration parameter 
    +
    " and ([System.Links.LinkType]='System.LinkTypes.Hierarchy-Forward')" +
    " ORDER BY [System.Id] " +
    " mode (Recursive)", projectname, Iteration);

    }
Posted
Updated 1-Apr-18 22:13pm
v2

1 solution

You could use a StringBuilder instead of the String.Format. That way you can assert whether or not the value of Iteration is Null or Empty.
C#
public void GetProjectInfo(string projectname,string Iteration)
{
	var querystring = new StringBuilder("select [System.Id], [System.Title],[Story.Author],[Story.Owner],[System.AssignedTo],[System.WorkItemType],[Microsoft.VSTS.Scheduling.StoryPoints],[Microsoft.VSTS.Common.Priority],[Microsoft.VSTS.Scheduling.Effort], [Actual.Effort.Completed],[System.State],[System.IterationPath] FROM WorkItemLinks WHERE ");
	
	querystring.AppendFormat("([Source].[System.TeamProject]='{0}' and [Source].[System.WorkitemType] IN ('Feature', 'Bug', 'Product Backlog Item','Task') ", projectname);

    if (!string.IsNullOrEmpty(Iteration)){
		querystring.AppendFormat("AND [Source].[System.IterationPath] IN ('{1}')) ", Iteration);
	}
	
	querystring.Append(" AND ([System.Links.LinkType]='System.LinkTypes.Hierarchy-Forward') ORDER BY [System.Id] mode (Recursive)");

	//use querystring.ToString() as your SQL statement

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900