简体   繁体   English

有没有办法像这样过滤 C# DataView?

[英]Is there any way to filter C# DataView like this?

I am writing some code for filtering data in BindingSource in c#.我正在编写一些代码,用于在 c# 中的BindingSource中过滤数据。

In DataTable , I have one column and data is something like this,DataTable ,我有一列,数据是这样的,

coding is very good编码非常好

I want to filter data from string after every empty space like my example,我想像我的例子一样在每个空格之后过滤字符串中的数据,

var ss = "coding is      ery"; 
var s = ss.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

var filter = ColumnName + " like \'%" + s[0] + "%\' ";

        for (int i = 1; i < s.Length; i++)
        {
            filter = filter +  "AND " + ColumnName + " like \'%" + s[i] + "%\' ";
        }

BindingSource.Filter = filter;

The problem here is that I will get all the data ( coding is very good ).这里的问题是我将获得所有数据(编码非常好)。 I don't want to get the data when word is not starting with the correct letter.当单词不是以正确的字母开头时,我不想获取数据。 Example,例子,

coding is ery.编码很糟糕。

I want to be like我想像

"coding very is" or "is very coding". “编码非常”或“非常编码”。

I want correct starting letters but position of word doesn't matter.我想要正确的起始字母,但单词的位置并不重要。

Does anybody have an idea how to do that?有没有人知道如何做到这一点?

You need to check it this way:你需要这样检查:

var filter = "(" + columnName
    + " like \'" + s[0] + "%\' "          // first word
    + " OR like \'% " + s[0] + "%\' )";   //in a sentence

for (int i = 1; i < s.Length; i++)
{
    filter += "AND " + "(" + columnName
    + " like \'" + s[i] + "%\' "          // first word
    + " OR like \'% " + s[i] + "%\' )";   //in a sentence
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM