有没有办法像这样过滤 C# DataView?
Is there any way to filter C# DataView like this?
我正在用 c# 在 BindingSource
中编写一些用于过滤数据的代码。
在DataTable
中,我有一列,数据是这样的,
coding is very good
我想在每个空 space 之后过滤字符串中的数据,就像我的例子,
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;
这里的问题是我会得到所有的数据(编码很好)。当单词不是以正确的字母开头时,我不想获取数据。例如,
coding is ery.
我想成为
"coding very is" or "is very coding".
我想要正确的起始字母,但单词的位置无关紧要。
有人知道怎么做吗?
您需要这样检查:
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
}
我正在用 c# 在 BindingSource
中编写一些用于过滤数据的代码。
在DataTable
中,我有一列,数据是这样的,
coding is very good
我想在每个空 space 之后过滤字符串中的数据,就像我的例子,
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;
这里的问题是我会得到所有的数据(编码很好)。当单词不是以正确的字母开头时,我不想获取数据。例如,
coding is ery.
我想成为
"coding very is" or "is very coding".
我想要正确的起始字母,但单词的位置无关紧要。
有人知道怎么做吗?
您需要这样检查:
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
}