我需要一些帮助来过滤 SQL WIQL查询。如何修改下面的代码以忽略具有空模型值的字段?例如,如果用户只想过滤一个字段(如工作项类型)而忽略状态字段?任何帮助将不胜感激。
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");
}
}
答案 0 :(得分:0)
只需删除此代码&& [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");
}
}
更好更清晰的方式
Wiql query = new Wiql() { Query = $"SELECT [Id], [Title] FROM workitems WHERE [Work Item Type] = {model.state}"};