寻找模式.

我需要在列表中搜索是否存在某种模式。


var result=roles.Where(z=>z.Contains(x) && z.Contains(y)).ToList();


string x = "Resource:resource1:resource2";

string y = "writer";

List<string> roles=new List<string>{"Resource::reader","Resource:resource1::Deleter","Resource:resource1::writer"};

我需要查找角色列表中是否存在任何值,例如:


Resource::writeror Resource:resource1::writeror Resource:resource1:resource2::writer ie 基于 : 分割 x 并将 y 附加到分割 x 的组合


眼眸繁星
浏览 114回答 3
3回答

烙印99

如果我对你的问题的理解是正确的:您有一个列表,其中可以包含您命名的角色的任何内容。这些角色的格式为 A::B 或 A:B::C 或 A:B:C::D 等...您想要实现的是查找来自 x 的任何“路径”或路径组合是否可以赋予角色 y ?例如:如果您有类似 A::ZA::YA:B::XA:B:C::X 的角色你有 x ,即 A:B:Cy 是 X你想检查列表中是否有 A::X如果你不这样做,你要检查列表中的 A:B::X,如果你仍然不知道,你会寻找 A:B:C::X所以,如果我是对的,你可以考虑这样的事情:&nbsp; &nbsp; &nbsp; String path = "A:B:C";&nbsp; &nbsp; &nbsp; &nbsp; String roleNeeded = "X";&nbsp; &nbsp; &nbsp; &nbsp; List<String> roles = new List<string>() { "A::Z", "A::Y", "A:B::X" };&nbsp; &nbsp; &nbsp; &nbsp; List<String> pathStep = new List<string>();&nbsp; &nbsp; &nbsp; &nbsp; pathStep = path.Split(':').ToList();&nbsp; &nbsp; &nbsp; &nbsp; String lookupPath = String.Empty;&nbsp; &nbsp; &nbsp; &nbsp; String result = String.Empty;&nbsp; &nbsp; &nbsp; &nbsp; pathStep.ForEach( s =>&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lookupPath += s;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (roles.Contains(lookupPath + "::" + roleNeeded))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = lookupPath + "::" + roleNeeded;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lookupPath += ":";&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; if (result != String.Empty)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // result is Good_Path::Role&nbsp; &nbsp; &nbsp; &nbsp; }这样,您开始将路径 X 拆分为列表,并将其聚合在 foreach 中以查看每个步骤。

守着一只汪

您应该考虑使用正则表达式。试试这个,string x = "Resource:resource1:resource2";string y = "writer";List<string> roles;List<string> words&nbsp; &nbsp;= new List<string> { x, y };// We are using escape to search for multiple strings.string&nbsp; &nbsp; &nbsp; &nbsp;pattern = string.Join("|", words.Select(w => Regex.Escape(w)));Regex&nbsp; &nbsp; &nbsp; &nbsp; regex&nbsp; &nbsp;= new Regex(pattern, RegexOptions.IgnoreCase);// You got matched results...List<string> matchedResults = roles.Where(regex.IsMatch).ToList();

米琪卡哇伊

&nbsp; &nbsp; &nbsp; string x = "Resource:resource1:resource2";&nbsp; &nbsp; &nbsp; &nbsp; string y = "writer";&nbsp; &nbsp; &nbsp; &nbsp; List<string> roles = new List<string>&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Resource::writer",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Resource:resource1:resource2::writer"&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; var records = x.Split(':').Select((word, index) => new { word, index });&nbsp; &nbsp; &nbsp; &nbsp; var result =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from record in records&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let words = $"{string.Join(":", records.Take(record.index + 1).Select(r => r.word))}::{y}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; join role in roles on words equals role&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select words;
打开App,查看更多内容
随时随地看视频慕课网APP