0

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.

    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);

    }
3
  • What you mean by string iteration will come .. ? Commented Apr 1, 2018 at 9:38
  • means if iteration parameter value ="" or NULL Commented Apr 1, 2018 at 9:40
  • Do you know that you could remove all that messy + and "" just prepending the @ character in front to the opening quote of the string? Commented Apr 1, 2018 at 9:44

1 Answer 1

1

Try

public void GetProjectInfo(string projectname, string Iteration)
{
    var query =
            $@"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]='{projectname}' 
                   AND [Source].[System.WorkitemType] IN ('Feature', 'Bug', 'Product Backlog Item', 'Task') 
                   ${(string.IsNullOrEmpty(Iteration) ? "" : $"AND [Source].[System.IterationPath] IN ('{Iteration}'))") }
                   AND ([System.Links.LinkType]='System.LinkTypes.Hierarchy-Forward')
            ORDER BY [System.Id] mode (Recursive)";
}

Note

string concatenation is a bad practice for building a SQL Query, there is a thread of SQL Injection

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.