【问题标题】:Match a single character in a DataView RowFilter?匹配 DataView RowFilter 中的单个字符?
【发布时间】:2012-05-09 17:11:25
【问题描述】:

我想知道是否有任何可能的方法在提供单字符匹配的DataView 上应用RowFilter

例如DataView.RowFilter = "SOME_COL like 'A_BCD%';

其中下划线表示 Oracle SQL 中的单个字符匹配,% 是 0 个或多个字符的通配符。

显然 RowFilter 仅支持通配符(* or %) 在匹配表达式的开头或结尾。我唯一的另一个想法是遍历行并删除与正则表达式不匹配的行,但这不理想。

任何帮助或想法将不胜感激! 谢谢

【问题讨论】:

    标签: c# asp.net regex dataview rowfilter


    【解决方案1】:

    或者,

    DataView.RowFilter = "Substring(SOME_COL, 1, 1) = 'A' and Substring(SOME_COL, 3, 3) = 'BCD';"
    

    【讨论】:

    • 有趣 - 我不知道您可以编写一个字符串表达式来评估行过滤器。最后的括号不匹配是否有原因?
    • @russdot 来自复制和粘贴测试的错字。固定。
    【解决方案2】:

    您可以使用 Linq to Datasets 来执行此操作

    class Program
    {
    
        static void Main(string[] args)
        {
            DataTable dt = new DataTable("test");
            dt.Columns.Add("SOME_COL", typeof(string));
    
            dt.Rows.Add("AABCDEFG");
            dt.Rows.Add("AZBCDEFG");
            dt.Rows.Add("AZZBCDEFG");
            dt.Rows.Add("ABC");
    
            Regex r = new Regex("A\\wBCDE");
    
            DataView dv 
               = (from t in dt.AsEnumerable()
                  where r.IsMatch(t.Field<string>("SOME_COL"))
                 select t).AsDataView();
    
            foreach(DataRowView row in dv)
            {
                Console.WriteLine(row[0]);
            }
    
    
        }
    
    
    }
    

    输出

    AABCDEFG
    AZBCDEFG
    Press any key to continue . . .
    

    【讨论】:

      猜你喜欢
      • 2011-09-20
      • 1970-01-01
      • 1970-01-01
      • 2017-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      • 1970-01-01
      相关资源
      最近更新 更多