在 DataView 行中查找下一个更高的值

Find next higher value in DataView row

我有一个 DataView,它在行 PathName 中包含如下名称:

"C:$Recycle.Bin\S-1-5-21-1993492240-2256127134-121505747-1000"
"C:$Recycle.Bin\S-1-5-21-1993492240-2256127134-121505747-1004"
"C:\Program Files (x86)\Common Files\microsoft shared\DAO\dao360.dll"
"C:\Program Files (x86)\Common Files\microsoft shared\ink.0\Microsoft.Ink.dll"
"C:\Program Files (x86)\Common Files\microsoft shared\ink\de-DE\InkObj.dll.mui"
"C:\Windows\System32\de-DE\Query.dll.mui"
"C:\Windows\System32\de-DE\query.exe.mui"
"C:\Windows\System32\de-DE\quser.exe.mui"
"C:\Windows\System32\de-DE\Qutil.dll.mui"
"C:\Windows\System32\DriverStore\FileRepository\bth.inf_amd64_neutral_de0494b6391d872c\bthport.sys"
"C:\Windows\System32\DriverStore\FileRepository\bth.inf_amd64_neutral_de0494b6391d872c\BTHUSB.SYS"
"C:\Windows\System32\DriverStore\FileRepository\bth.inf_amd64_neutral_e54666f6a3e5af91\bthenum.sys"

对于任何给定路径,我需要获取子文件夹。

路径的子文件夹 "C:\"

"$Recycle.Bin"
"Program Files (x86)"
"Windows"

我目前正在做的是:

DataView 可能包含 100.000 条或更多条目,因此这种方式需要很长时间。

一种可能性是在每次命中后扩展行过滤器:

MyDataView.rowfilter += " and (PathName LIKE 'c:\%')" + " and (PathName NOT LIKE 'C:$Recycle.Bin\%')". 

但是路径名可能会变得很长,这将导致行过滤器中的堆栈溢出。

有没有其他方法,比如"get the next higher value"?

编辑
必须为 any 给定路径找到子文件夹,而不仅仅是 c:\

如果我对你的理解正确的话,这可以用一行 Linq 来完成 (为了便于阅读,分成多行)

var dirs = dv.Table
             .AsEnumerable()
             .Select(x => Path.GetDirectoryName(x.Field<string>("PathName")))
             .Distinct();

技巧在于使用 Path.GetDirectoryName 从每一行中提取字段 PathName 的值,并使用 Select 方法创建一个可枚举的字符串。然后 Distinct 方法 returns 只是集合中的唯一目录名称。

如果您的 DataView 已针对某些其他条件进行了筛选,并且您希望保留此条件以排除一部分记录,那么您可以使用 Where 方法来提取所需的记录。例如,要排除以 "C:\WINDOWS" 开头的每条记录,您可以编写

var dirs = dv.Table
             .AsEnumerable()
             .Where(k => !k.Field<string>("PathName").StartsWith(@"C:\WINDOWS"))
             .Select(x => Path.GetDirectoryName(x.Field<string>("PathName")))
             .Distinct();