简体   繁体   English

在ASP.NET C中使用Windcard的Datatable -Row过滤器

[英]Datatable -Row Filter using Windcard in asp.net c#

I have datatable which has data as below now i need to fileter the rows based on column Role which contains the value 5:4 but when i filter through dataview rowfilter i am getting rows with values 75:4 also which is wrong below is the code i have tried , could anyone help me filter the particular value using dataview 我有具有以下数据的数据表,现在我需要根据包含值5:4的角色列对行进行过滤,但是当我通过dataview rowfilter进行过滤时,我得到的值是75:4的行也是错误的,下面是代码我试过了,有人可以帮我使用dataview过滤特定值

在此处输入图片说明

datatabl;e looks like above datatabl; e看起来像上面

and below is c# code that i tried to filter 以下是我尝试过滤的C#代码

DataView dv1 = new DataView(dt);
                dv1.RowFilter = "Role like'%5:4%'";
                dt1 = dv1.ToTable();

Don't use RowFilter which is very limited and outdated but Linq-To-DataTable: 不要使用非常有限且过时的RowFilter ,但不要使用Linq-To-DataTable:

var matchingRows = from row in dt.AsEnumerable()
                   let roles = row.Field<string>("Role").Split('/')
                   where roles.Contains("5:4") 
                   select row;

This approach first splits the string by / and then searches in this array with Contains . 此方法首先用/分割字符串,然后使用Contains在此数组中搜索。

If you need to know if there was a matching row: 如果您需要知道是否存在匹配的行:

bool hasMatchingRows = matchingRows.Any();

if you want to create a new DataTable with those mathing rows: 如果要使用这些数学行创建一个新的DataTable:

if( hasMatchingRows )
{
    DataTable tblMatches = matchingRows.CopyToDataTable();
}

If you just want to know the initial-column of the matching.rows: 如果您只想知道match.rows的初始列:

List<string> initials = matchingRows.Select(r => r.Field<string>("Initial")).ToList();

which contains only NML and GGRA in this example. 在此示例中仅包含NMLGGRA

replace 更换

dv1.RowFilter = "Role like'%5:4%'";

with

dv1.RowFilter = "Role like '5:4/%' OR Role like '%/5:4' OR Role = '5:4' OR Role like '%/5:4/%'";
//                             ^                   ^                ^                    ^
//                        startswith            endswith         equals              contains

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

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