【问题标题】:RowFilter LIKE operationRowFilter LIKE 操作
【发布时间】:2011-02-12 23:58:55
【问题描述】:

我知道以下不允许作为行过滤器

'canada%.txt' 或'canada*.txt'

我想我可以将过滤器重写为

file_name like 'Canada%' and file_name like '%.txt' 

应该可以。

但是有没有更简单的方法来代替确定 % 的位置并拆分字符串?

【问题讨论】:

    标签: c# datarowview


    【解决方案1】:

    我不相信过滤器表达式中允许使用 CHARINDEX。

    您可能会尝试从 C# 动态构建过滤器字符串(非常未经测试,但这是一种可能的语法):

    //split the original string using the wildcard as the delimiter
    string[] f = s.Split('%');
    
    //use the resulting array to build the resultstring
    string resultstring = 'file_name like "' + f[0] + '%" and file_name like "%' + f[1] + '"'
    
    //view.RowFilter = resultstring;
    

    【讨论】:

    • 我想我忘了在那里转义一些引号,但希望你能明白……希望它对你有用……
    • 这就是我现在正在做的事情,将函数拆分为 * 或 % 并重建它。我只是想知道是否有一个很酷的 linq 函数可以应用于表格。
    【解决方案2】:

    这是我想出的解决方案

        private string CreateRowFilter(string remoteFilePattern)
        {
            string[] pattern = remoteFilePattern.Split(new char[] { '*', '%' });
            int length = pattern.GetUpperBound(0);
    
            if (length == 0)
            {
                return String.Format("Name = '{0}'", pattern[0]);
    
            }
    
            StringBuilder fileter = new StringBuilder(
                     String.Format("Name LIKE '{0}*' ", pattern[0]));
    
            for (int segment = 1; segment < length; segment++)
            {
                fileter.Append(
                     String.Format("AND Name LIKE '*{0}*' ", pattern[segment]));
            }
    
            if(String.IsNullOrEmpty(pattern[length]) == false)
            {
                fileter.Append(
                     String.Format("AND Name LIKE '*{0}' ", pattern[length]));
            }
    
            return fileter.ToString();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-05
      • 1970-01-01
      相关资源
      最近更新 更多