I need some help filtering a SQL WIQL query. How can I modify the code below to ignore the fields that have empty model values? For example if the user only wants to filter one field like the work item type and ignore the state field? Any help would be appreciated.
public async Task<ActionResult> Method(filterModel model)
{
VssConnection connection = new VssConnection(new
Uri(vstsCollectionUrl), new VssClientCredentials());
WorkItemTrackingHttpClient witClient = connection.GetClient<WorkItemTrackingHttpClient>();
Wiql query = new Wiql() { Query = "SELECT [Id], [Title] FROM workitems WHERE [Work Item Type] = '" + model.workitem + "' &&
[State] = '"+ model.state + "'"};
WorkItemQueryResult queryResults = witClient.QueryByWiqlAsync(query).Result;
if (queryResults == null || queryResults.WorkItems.Count() == 0)
{
Console.WriteLine("Query did not find any results");
}
}
Just remove this code && [State] = '"+ model.state + "'
:
public async Task<ActionResult> Method(filterModel model)
{
VssConnection connection = new VssConnection(new
Uri(vstsCollectionUrl), new VssClientCredentials());
WorkItemTrackingHttpClient witClient = connection.GetClient<WorkItemTrackingHttpClient>();
Wiql query = new Wiql() { Query = "SELECT [Id], [Title] FROM workitems WHERE [Work Item Type] = '" + model.state + "'" };
WorkItemQueryResult queryResults = witClient.QueryByWiqlAsync(query).Result;
if (queryResults == null || queryResults.WorkItems.Count() == 0)
{
Console.WriteLine("Query did not find any results");
}
}
Better and clear way:
Wiql query = new Wiql() { Query = $"SELECT [Id], [Title] FROM workitems WHERE [Work Item Type] = {model.state}"};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With