3 回答

TA貢獻1829條經驗 獲得超13個贊
如果我對你的問題的理解是正確的:
您有一個列表,其中可以包含您命名的角色的任何內容。這些角色的格式為 A::B 或 A:B::C 或 A:B:C::D 等...
您想要實現的是查找來自 x 的任何“路徑”或路徑組合是否可以賦予角色 y ?
例如:如果您有類似 A::ZA::YA:B::XA:B:C::X 的角色
你有 x ,即 A:B:C
y 是 X
你想檢查列表中是否有 A::X
如果你不這樣做,你要檢查列表中的 A:B::X,
如果你仍然不知道,你會尋找 A:B:C::X
所以,如果我是對的,你可以考慮這樣的事情:
String path = "A:B:C";
String roleNeeded = "X";
List<String> roles = new List<string>() { "A::Z", "A::Y", "A:B::X" };
List<String> pathStep = new List<string>();
pathStep = path.Split(':').ToList();
String lookupPath = String.Empty;
String result = String.Empty;
pathStep.ForEach( s =>
{
lookupPath += s;
if (roles.Contains(lookupPath + "::" + roleNeeded))
{
result = lookupPath + "::" + roleNeeded;
}
lookupPath += ":";
});
if (result != String.Empty)
{
// result is Good_Path::Role
}
這樣,您開始將路徑 X 拆分為列表,并將其聚合在 foreach 中以查看每個步驟。

TA貢獻1872條經驗 獲得超4個贊
您應該考慮使用正則表達式。試試這個,
string x = "Resource:resource1:resource2";
string y = "writer";
List<string> roles;
List<string> words = new List<string> { x, y };
// We are using escape to search for multiple strings.
string pattern = string.Join("|", words.Select(w => Regex.Escape(w)));
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
// You got matched results...
List<string> matchedResults = roles.Where(regex.IsMatch).ToList();

TA貢獻1998條經驗 獲得超6個贊
string x = "Resource:resource1:resource2";
string y = "writer";
List<string> roles = new List<string>
{
"Resource::writer",
"Resource:resource1:resource2::writer"
};
var records = x.Split(':').Select((word, index) => new { word, index });
var result =
from record in records
let words = $"{string.Join(":", records.Take(record.index + 1).Select(r => r.word))}::{y}"
join role in roles on words equals role
select words;
- 3 回答
- 0 關注
- 164 瀏覽
添加回答
舉報