C# DataTable Select 需要找的内容带%解决办法

 DataRow[] rows;

 if (originalValue.Contains("%"))
 {
     DataRow[] tempRows;
     string leftString = originalValue.Substring(0, originalValue.IndexOf("%"));
     if(string.IsNullOrEmpty(leftString))
     {
         string rightString= originalValue.Substring(originalValue.IndexOf("%")+1);
         tempRows = viewModel.DataGridViewTestProcedureDataSource.Select($"Parameter like '%{rightString}%'");
     }
     else
     {
         tempRows = viewModel.DataGridViewTestProcedureDataSource.Select($"Parameter like '%{leftString}%'");
     }
     List listRows = new List();
     foreach (DataRow row in tempRows)
     {
         if (row["Parameter"].ToString().Contains(originalValue))
         {
             listRows.Add(row);
         }
     }
     rows = listRows.ToArray();
 }
 else
 {
     DataRow[] middle = viewModel.DataGridViewTestProcedureDataSource.Select($"Parameter like '%,{originalValue},%'");//被夹在中间的
     DataRow[] head = viewModel.DataGridViewTestProcedureDataSource.Select($"Parameter like '{originalValue},%'");//开头的
     DataRow[] tail = viewModel.DataGridViewTestProcedureDataSource.Select($"Parameter like '%,{originalValue}'");//结尾的
     DataRow[] total = viewModel.DataGridViewTestProcedureDataSource.Select($"Parameter = '{originalValue}'");//只有这一个参数

      rows = middle.Union(head).Union(tail).Union(total).ToArray();//Union方法可以返回两个数组的集合,并去掉相同的元素
 }

你可能感兴趣的:(c#,windows,开发语言)