首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

BindingSource.Filter 怎么实现模糊查询

2012-04-22 
BindingSource.Filter 如何实现模糊查询希望通过Filter属性进行过滤BindingSource.Filter %p//okBind

BindingSource.Filter 如何实现模糊查询
希望通过Filter属性进行过滤
BindingSource.Filter = "%p"; //ok
BindingSource.Filter = "%p%"; //ok
BindingSource.Filter = "%p%c%"; //wrong,提示表达式错误
如果希望进行第三种方式的模糊查询,应该怎么办?

[解决办法]
刚才用Reflector看了下微软的实现,可以看出来的确不允许中间有通配符。

C# code
internal string AnalizePattern(string pat)        {            int length = pat.Length;            char[] destination = new char[length + 1];            pat.CopyTo(0, destination, 0, length);            destination[length] = '\0';            string str = null;            char[] chArray2 = new char[length + 1];            int num3 = 0;            int num4 = 0;            int index = 0;            while (index < length)            {                if ((destination[index] == '*') || (destination[index] == '%'))                {                    while (((destination[index] == '*') || (destination[index] == '%')) && (index < length))                    {                        index++;                    }                    if (((index < length) && (num3 > 0)) || (num4 >= 2))                    {                        throw new Exception("ExprException.InvalidPattern(pat);");                    }                    num4++;                }                else if (destination[index] == '[')                {                    index++;                    if (index >= length)                    {                        throw new Exception("ExprException.InvalidPattern(pat);");                    }                    chArray2[num3++] = destination[index++];                    if (index >= length)                    {                        throw new Exception("ExprException.InvalidPattern(pat);");                    }                    if (destination[index] != ']')                    {                        throw new Exception("ExprException.InvalidPattern(pat);");                    }                    index++;                }                else                {                    chArray2[num3++] = destination[index];                    index++;                }            }            str = new string(chArray2, 0, num3);            if (num4 == 0)            {                kind = 4;                return str;            }            if (num3 > 0)            {                if ((destination[0] == '*') || (destination[0] == '%'))                {                    if ((destination[length - 1] == '*') || (destination[length - 1] == '%'))                    {                        kind = 3;                        return str;                    }                    kind = 2;                    return str;                }                kind = 1;                return str;            }            kind = 5;            return str;        } 

热点排行