开发者

RowFilter LIKE operation

开发者 https://www.devze.com 2022-12-28 14:33 出处:网络
I know that the following is not allowed as a row filter \'canada%.txt\' or \'canada*.txt\' and I guess I can rewrite my filter as

I know that the following is not allowed as a row filter

'canada%.txt' or 'canada*.txt'

and I guess I can rewrite my filter as

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

should work.

But is there a开发者_JAVA百科n easier way instead of determing where the % is and spliting the string?


I don't believe that CHARINDEX is allowed in the filter expression.

You might try to dynamically build the filter string from C# (very much untested, but here's a possible syntax):

//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;


Here is the solution that I came up with

    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();
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号