使用“Like”运算符从DataTable的Select方法获取数据时出错

时间:2012-01-06 06:14:09

标签: c# .net wpf datatable

我正在使用以下代码

foreach (KeyValuePair<string, string> kvp in letGrdSignList)
                {
                    //Check if added function in definition is modified, if yes then don't add it in signature list
                    DataRow[] dLetRow = dtLet.Select("Definition" + "Like"" + "'" +
                        "%"+ kvp.Value + "%" + "'");
                   //kvp.value contains  "local:try2values($arg1 as xs:decimal,$arg2 as xs:float*)"     
                   //Above line results in error

                   if (dLetRow.Length > 0)
                    {
                        //Check if signature already exists
                        if (!strList.ContainsKey(kvp.Key))
                            strList.Add(kvp.Key, kvp.Value);
                    }
                    //else
                    //    //Remove from list if signature is not present in any defination column
                    //    letGrdSignList.Remove(sLetSignature);ss
                }


I am getting following error
{System.Data.EvaluateException: Error in Like operator: the string pattern '%local:try2values($arg1 as xs:decimal,$arg2 as xs:float*)%' is invalid.
   at System.Data.LikeNode.AnalyzePattern(String pat)
   at System.Data.LikeNode.Eval(DataRow row, DataRowVersion version)
   at System.Data.Select.AcceptRecord(Int32 record)
   at System.Data.Select.GetLinearFilteredRows(Range range)
   at System.Data.Select.SelectRows()
   at System.Data.DataTable.Select(String filterExpression)
   at WPFApp.QueryGenerator.GetSignatureList()

代码截图

如果发现这个,请从MSDN

通配符

在LIKE比较中,*和%可以互换地用于通配符。如果LIKE子句中的字符串包含*或%,则这些字符应括在括号([])中。如果括号在子句中,则每个括号字符应括在括号中(例如[[]或[]])。允许在模式的开始和结束处,或在模式的结尾处或在模式的开始处使用通配符。例如:

“ItemName LIKE' product '”

“ItemName LIKE'* product'”

“ItemName LIKE'产品*'”

字符串中间不允许使用通配符。例如,不允许使用'te * xt'。

2 个答案:

答案 0 :(得分:2)

使用Like运算符

时需要转义“%”
//Check if added function in definition is modified, if yes then don't add it in signature list
    DataRow[] dLetRow = dtLet.
       Select("Definition Like '[%]%" + kvp.Value + "[%]%'");

答案 1 :(得分:0)

我用这个替换了

 DataRow[] dLetRow = dtLet.Select("Definition" + "Like"" + "'" +
                        "%"+ kvp.Value.replace("*","[*]") + "%" + "'");

这解决了这个问题,但我不确定这种方法是否正确