.NET BindingSource.Filter 与正则表达式 [英] .NET BindingSource.Filter with regular expressions

查看:68
本文介绍了.NET BindingSource.Filter 与正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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*

谢谢!

推荐答案

您可以非常轻松地使用 LINQ 查询 DataTable,然后您可以在查询中使用实际的 Regex 以任何您喜欢的方式对其进行过滤.

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.

这样的东西...

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

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

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

这篇关于.NET BindingSource.Filter 与正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆