C# .NET BindingSource.Filter 与正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/819526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.NET BindingSource.Filter with regular expressions
提问by clamp
i am using a BindingSource.Filter to list only certain elements of the datasource. especially i use it like this a lot:
我正在使用 BindingSource.Filter 来仅列出数据源的某些元素。特别是我经常这样使用它:
m_bindingSourceTAnimation.Filter = "Name LIKE '" + FilterText + "'";
now my question is, if it is somehow possible to use regular expressions with these filters.
现在我的问题是,是否可以在这些过滤器中使用正则表达式。
i would especially need multiple wildcard (*) characters like
我特别需要多个通配符 (*) 字符,例如
*hello*world*
thanks!
谢谢!
采纳答案by Brian ONeil
You can query the DataTable with LINQ pretty easily and then you can use a actual Regex within the query to filter it anyway you like.
您可以非常轻松地使用 LINQ 查询 DataTable,然后您可以在查询中使用实际的 Regex 以任何您喜欢的方式对其进行过滤。
Something like this...
像这样的东西...
var source = myDataTable.AsEnumerable();
var results = from matchingItem in source
where Regex.IsMatch(matchingItem.Field<string>("Name"), "<put Regex here>")
select matchingItem;
//If you need them as a list when you are done (to bind to or something)
var list = results.ToList();
This will get you the rows that match based on an actual Regex, I don't know what you need to do with the information, but this would allow you to get the rows based on a Regex.
这将为您提供基于实际正则表达式匹配的行,我不知道您需要对这些信息做什么,但这将允许您根据正则表达式获取行。
****Update** - Trying to clarify based on comment
****更新** - 试图根据评论澄清
I don't know what you are using this for so I don't have a great context, but from what I can guess you are using a DataTable to data bind to a Grid or something like that. If this is the case, I think that you should be able to assign "list" from the snippet I put in here as the DataSource (assuming you are using a BindingSource) and I think that it will work. I don't use DataTables, I usually stick to objects for working with my data so I am not exactly sure how it will handle the list of rows, but I would think that it would work (or be close enough that a little google searching would do it).
我不知道你用它做什么,所以我没有很好的上下文,但是从我猜你是使用 DataTable 将数据绑定到 Grid 或类似的东西。如果是这种情况,我认为您应该能够从我放在这里的代码片段中分配“列表”作为数据源(假设您使用的是 BindingSource),并且我认为它会起作用。我不使用 DataTables,我通常坚持使用对象来处理我的数据,所以我不确定它将如何处理行列表,但我认为它会起作用(或者足够接近一些谷歌搜索会做的)。
回答by Marc Gravell
BindingSource
relies on IBindingListView.Filter
for this functionality. The behaviour depends entirelyon the specific list implementation. Is this a DataTable
/DataView
? If so, this maps to DataView.RowFilter
, with syntax listed here.
BindingSource
依赖于IBindingListView.Filter
这个功能。行为完全取决于特定的列表实现。这是DataTable
/DataView
吗?如果是,则映射到DataView.RowFilter
,语法在此处列出。
The DataView
implementation has no regex support, but supports LIKE
via *
- i.e. where FilterText
is something like "Foo*Bar*"
. At least, that is my understanding.
该DataView
实现没有正则表达式支持,但支持LIKE
通过*
- 即 whereFilterText
是类似"Foo*Bar*"
. 至少,这是我的理解。
I'm still assuming that you are using DataTable
/DataView
... a pragmatic alternative might be to introduce an extra (bool) column for the purpose. Set/clear that marker as the predicate (using a regex or any other complicated logic), and just use the row-filter to say "where set". Not very clean, maybe, but a lot simpler than implementing a custom data-view / binding-source.
我仍然假设您正在使用DataTable
/ DataView
... 一个实用的替代方法可能是为此目的引入一个额外的 (bool) 列。将该标记设置/清除为谓词(使用正则表达式或任何其他复杂的逻辑),然后仅使用行过滤器说出“设置的位置”。也许不是很干净,但比实现自定义数据视图/绑定源要简单得多。
If you are using objects (rather than DataTable
), then another option might be the Dynamic LINQ Library. I don't know the full range of what it supports, but it (Where(string)
) certainly has some / much of the RowFilter
capability. And since the code is available in the sample project, it is possibleyou could educate it to apply a regex?
如果您使用的是对象(而不是DataTable
),那么另一个选项可能是Dynamic LINQ Library。我不知道它支持的全部内容,但它 ( Where(string)
) 肯定具有某些/大部分RowFilter
功能。而且,由于该代码可以在示例项目中,这是可能的,你可以教它申请一个正则表达式?
回答by Brian ONeil
The comment below doesn't really work:
下面的评论并没有真正起作用:
"...a pragmatic alternative might be to introduce an extra (bool) column for the purpose. Set/clear that marker as the predicate (using a regex or any other complicated logic), and just use the row-filter to say "where set". Not very clean, maybe, but a lot simpler than implementing a custom data-view / binding-source."
“...一种实用的替代方法可能是为此目的引入一个额外的(布尔)列。将该标记设置/清除为谓词(使用正则表达式或任何其他复杂的逻辑),然后使用行过滤器说“ where set”。也许不是很干净,但比实现自定义数据视图/绑定源要简单得多。”
When you set the new column this causes the row state to change and you basically end up with the whole table/dataview thinking that it needs to do every row in the next update. Not sure how to get around this problem.
当您设置新列时,这会导致行状态发生变化,并且您基本上最终会认为整个表/数据视图需要在下一次更新中执行每一行。不知道如何解决这个问题。
回答by bsj
I solved this issue by splitting the search string by the wildcard and then created the row filter expression using the split values.
我通过按通配符拆分搜索字符串解决了这个问题,然后使用拆分值创建了行过滤器表达式。
Array a = SearchString.Split('*');
string rowFilter = "";
if (a.GetUpperBound(0) == 1)
{
rowFilter = "(MODEL_NBR like '" + a.GetValue(0).ToString() + "*' AND MODEL_NBR like '*" + a.GetValue(1).ToString() + "')";
}
If you use multiple wildcards you could create a recursive function that creates the filter expressio
如果您使用多个通配符,您可以创建一个递归函数来创建过滤器表达式
回答by Max
'[Description] Is column name
Dim SearchStrArr() As String = Split(txtSearch.Text, " ")
Dim FilterString As String = ""
FilterString = String.Join("%' AND [Description] Like '%", SearchStrArr)
FilterString = "[Description] Like '%" & FilterString & "%'"
m_bindingSourceTAnimation.Filter = FilterString