使用 C# 过滤 WIQL 查询

Filtering a WIQL Query with C#

我需要一些帮助来过滤 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");          
        }
    }

只需删除此代码 && [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}"};