更新时间:2023-02-11 13:24:00
如果你的目标是获取一个列表(在这种情况下,是FileNames)并快速确定哪些项目(在这种情况下,字符串)可以根据某些标准排除(在这种情况下,一组中的匹配)字符串),考虑这样的事情:
If your goal is take a list (in this case, of FileNames) and quickly determine which items (in this case, strings) can be excluded based on some criterion (in this case, a match in a set of strings), consider something like this:
using System;
using System.Collections.Generic;
using System.Linq;
IgnoreFolders = new List<string>
{
"SYNC ISSUES",
"TRASH",
"JUNK",
};
TestNames = new List<string>
{
"SYNC ISSUES1",
"SYNC ISSUES2",
"sometrash", "trash more", "my trash is",
"myJUNK", "junkYOUR", "JUNKJUNKJUNK",
"good name 1", "june bug good name",
"tras hot good name", "jun k tras h"
};
var FileNamesToIgnore = TestNames
.Where(name => IgnoreFolders
.Any(itm => name.ToUpper()
.Contains(itm)));
var GoodToGoFileNames = TestNames.Except(FileNamesToIgnore);
您可以轻松地将此代码包装在一个函数中;为了重复使用,我建议在该函数中有一个布尔标志,用于确定是否应忽略letter-case(如示例中所示)。
You could easily wrap this code in a Function; for re-use, I'd suggest having a boolean flag in that function that determined whether letter-case should be ignored (as in the example, here).
bool IgnoreFolder(Folder folder)
{
String folderName = folder.Name.ToLower();
return IgnoreFolders.Any(x => folderName.Contains(x.ToLower()));
}
这将是LINQified版本:
This would be the LINQified version:
bool IgnoreFolder(Folder folder)
{
String FolderName = folder.Name.ToUpper();
return IgnoreFolders.Any(ign => FolderName.StartsWith(ign.ToUpper());
}