如何在 DataTable 中的分隔字段中查询精确值
How to query for exact value within a delimited field inside a DataTable
所以我有一个数据表,其中有一些字段是从 Rest 填充的(未全部列出),我有一个树视图,其中填充了 DT 中路径的描绘树。我希望能够 select 树的任何部分并生成所有匹配项的数组。我 运行 遇到的问题是,如果我搜索 M19,我会得到包含 M19 的所有内容……包括 M198。我可以通过解析路径并执行精确的操作来完成这项工作......但它真的很难看而且非常慢并且添加了一个全新的循环。有没有更优雅的编码方式?
Sample Code:
public string[] GetNodeID(string locName)
{
var results = from myRow in locationData.AsEnumerable()
where myRow.Field<string>("Path").ToUpper().Contains(locName.ToUpper())
select myRow;
DataView view = results.AsDataView();
return null;
}
Example Tree
Main
->M19
-> ->M19-I1
-> ->M19-I2
-> M198
-> -> M198-I1
-> -> M198-I2
locationData Table
ID Path Description
0 Main\M19 Null
1 Main\M19\M19-I1 Instrument 1
2 Main\M19\M19-I2 Instrument 2
3 Main\M198\M198-I1 Instrument 1
4 Main\M198\M198-I2 Instrument 2
您可以用 \
和 -
分隔符拆分 Path
字段以创建字符串数组并搜索给定分支或节点的子字符串:
// +
// using System.Linq;
public string[] GetTreeBranch(DataTable dt, string branch) => dt.AsEnumerable()
.Where(x => x.Field<string>("Path").Split('\', '-')
.Any(y => y.Equals(branch, StringComparison.OrdinalIgnoreCase)))
.Select(x => x.Field<string>("Path")).ToArray();
或者,使用 RegEx
在 Path
字段中搜索匹配项。将 \b
附加到给定分支以创建 whole-word-search 模式(即 m19\b
)以跳过其他分支,如 M198
。 Online Test.
// +
// using System.Text.RegularExpressions;
public string[] GetTreeBranch(DataTable dt, string branch) => dt.AsEnumerable()
.Where(x => Regex.IsMatch(
x.Field<string>("Path"), $@"{branch}\b", RegexOptions.IgnoreCase))
.Select(x => x.Field<string>("Path")).ToArray();
无论哪种方式,调用:
var s = "m19";
Console.WriteLine(string.Join(", ", GetTreeBranch(locationData, s)));
产量:
Main\M19, Main\M19\M19-I1, Main\M19\M19-I2
所以我有一个数据表,其中有一些字段是从 Rest 填充的(未全部列出),我有一个树视图,其中填充了 DT 中路径的描绘树。我希望能够 select 树的任何部分并生成所有匹配项的数组。我 运行 遇到的问题是,如果我搜索 M19,我会得到包含 M19 的所有内容……包括 M198。我可以通过解析路径并执行精确的操作来完成这项工作......但它真的很难看而且非常慢并且添加了一个全新的循环。有没有更优雅的编码方式?
Sample Code:
public string[] GetNodeID(string locName)
{
var results = from myRow in locationData.AsEnumerable()
where myRow.Field<string>("Path").ToUpper().Contains(locName.ToUpper())
select myRow;
DataView view = results.AsDataView();
return null;
}
Example Tree
Main
->M19
-> ->M19-I1
-> ->M19-I2
-> M198
-> -> M198-I1
-> -> M198-I2
locationData Table
ID Path Description
0 Main\M19 Null
1 Main\M19\M19-I1 Instrument 1
2 Main\M19\M19-I2 Instrument 2
3 Main\M198\M198-I1 Instrument 1
4 Main\M198\M198-I2 Instrument 2
您可以用 \
和 -
分隔符拆分 Path
字段以创建字符串数组并搜索给定分支或节点的子字符串:
// +
// using System.Linq;
public string[] GetTreeBranch(DataTable dt, string branch) => dt.AsEnumerable()
.Where(x => x.Field<string>("Path").Split('\', '-')
.Any(y => y.Equals(branch, StringComparison.OrdinalIgnoreCase)))
.Select(x => x.Field<string>("Path")).ToArray();
或者,使用 RegEx
在 Path
字段中搜索匹配项。将 \b
附加到给定分支以创建 whole-word-search 模式(即 m19\b
)以跳过其他分支,如 M198
。 Online Test.
// +
// using System.Text.RegularExpressions;
public string[] GetTreeBranch(DataTable dt, string branch) => dt.AsEnumerable()
.Where(x => Regex.IsMatch(
x.Field<string>("Path"), $@"{branch}\b", RegexOptions.IgnoreCase))
.Select(x => x.Field<string>("Path")).ToArray();
无论哪种方式,调用:
var s = "m19";
Console.WriteLine(string.Join(", ", GetTreeBranch(locationData, s)));
产量:
Main\M19, Main\M19\M19-I1, Main\M19\M19-I2