.NET BindingSource.Filter 与正则表达式

发布于 2024-07-19 00:47:24 字数 274 浏览 9 评论 0原文

我正在使用 BindingSource.Filter 仅列出数据源的某些元素。 特别是我经常这样使用它:

m_bindingSourceTAnimation.Filter = "Name LIKE '" + FilterText + "'";

现在我的问题是,是否可以在这些过滤器中使用正则表达式。

我特别需要多个通配符(*)字符,例如

*hello*world*

谢谢!

i am using a BindingSource.Filter to list only certain elements of the datasource.
especially i use it like this a lot:

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

那伤。 2024-07-26 00:47:24

您可以非常轻松地使用 LINQ 查询 DataTable,然后您可以在查询中使用实际的正则表达式来以您喜欢的方式过滤它。

像这样的东西...

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

这将为您提供基于实际正则表达式匹配的行,我不知道您需要如何处理这些信息,但这将允许您根据正则表达式获取行。

****更新** - 试图根据评论进行澄清,

我不知道你用它做什么,所以我没有很好的上下文,但从我可以猜测你正在使用 DataTable 来数据绑定网格或类似的东西。 如果是这种情况,我认为您应该能够从我在此处作为数据源放入的代码片段中分配“列表”(假设您使用的是 BindingSource),并且我认为它会起作用。 我不使用 DataTables,我通常坚持使用对象来处理我的数据,所以我不确定它将如何处理行列表,但我认为它会起作用(或者足够接近,以至于需要进行一些谷歌搜索会做的)。

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.

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).

怪我入戏太深 2024-07-26 00:47:24

BindingSource 依赖于 IBindingListView.Filter 用于此功能。 该行为完全取决于特定的列表实现。 这是DataTable/DataView吗? 如果是这样,则映射到 DataView.RowFilter,语法如下 此处

DataView 实现没有正则表达式支持,但通过 * 支持 LIKE - 即其中 FilterText 类似于 “Foo*Bar*”。 至少,这是我的理解。


我仍然假设您正在使用 DataTable/DataView...一个实用的替代方案可能是为此目的引入一个额外的 (bool) 列。 设置/清除该标记作为谓词(使用正则表达式或任何其他复杂的逻辑),然后仅使用行过滤器来表示“设置位置”。 也许不是很干净,但比实现自定义数据视图/绑定源简单得多。


如果您使用对象(而不是DataTable),那么另一个选项可能是动态 LINQ 库。 我不知道它支持的全部范围,但它 (Where(string)) 肯定具有一些/大部分 RowFilter 功能。 由于代码在示例项目中可用,因此您可能可以训练它应用正则表达式吗?

BindingSource relies on IBindingListView.Filter for this functionality. The behaviour depends entirely on the specific list implementation. Is this a DataTable/DataView? If so, this maps to DataView.RowFilter, with syntax listed here.

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.


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.


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 possible you could educate it to apply a regex?

不念旧人 2024-07-26 00:47:24

下面的评论实际上不起作用:

“......一个实用的替代方案可能是为此目的引入一个额外的(布尔)列。设置/清除该标记作为谓词(使用正则表达式或任何其他复杂的逻辑) ),然后使用行过滤器来表示“在哪里设置”,也许不是很干净,但比实现自定义数据视图/绑定源简单得多。”

当您设置新列时,导致行状态发生变化,并且您基本上最终会得到整个表/数据视图,认为它需要在下一次更新中执行每一行。
不知道如何解决这个问题。

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."

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.

放飞的风筝 2024-07-26 00:47:24

我通过用通配符拆分搜索字符串,然后使用拆分值创建行过滤器表达式来解决此问题。

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() + "')";

 }

如果您使用多个通配符,您可以创建一个递归函数来创建过滤器表达式

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

清醇 2024-07-26 00:47:24
    '[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 
    '[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 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文