C# 如何确定集合中的任何元素是否显示在具有LINQ的字符串中?
我有C# 如何确定集合中的任何元素是否显示在具有LINQ的字符串中?,c#,linq,C#,Linq,我有字典对象。键表示文件名,值是一个列表,它表示文件中某些方法的名称 我在字典上循环并使用键从文件中读取数据。然后,我尝试在该文件中查找包含值对象中的元素的行: static void FindInvalidAttributes(Dictionary<string, List<string>> dictionary) { //Get the files from my controller dir List<string> controller
字典
对象。键
表示文件名,值
是一个列表
,它表示文件中某些方法的名称
我在字典上循环并使用键
从文件中读取数据。然后,我尝试在该文件中查找包含值
对象中的元素的行:
static void FindInvalidAttributes(Dictionary<string, List<string>> dictionary)
{
//Get the files from my controller dir
List<string> controllers = Directory.GetFiles(controllerPath, "*.cs", SearchOption.AllDirectories).ToList<string>();
//Iterate over my dictionary
foreach (KeyValuePair<string, List<string>> entry in dictionary)
{
//Build the correct file name using the dictionary key
string controller = Path.Combine(ControllerPath, entry.Key + "Controller.cs");
if (File.Exists(controller))
{
//Read the file content and loop over it
string[] lines = File.ReadAllLines(controller);
for (int i = 0; i < lines.Count(); i++)
{
//loop over every element in my dictionary's value (List<string>)
foreach (string method in entry.Value)
{
//If the line in the file contains a dictionary value element
if (lines[i].IndexOf(method) > -1 && lines[i].IndexOf("public") > -1)
{
//Get the previous line containing the attribute
string verb = lines[i - 1];
}
}
}
}
}
}
使用上述数据,行[0]
应导致我的FindInvalidAttributes
方法查看前一行,因为此字符串包含myList
中的第一个元素<代码>行[1]不应导致方法检查前一行,因为SomeOtherMethod
不出现在myList
中
编辑我很好奇为什么投票被否决,并被标记为“过于宽泛”而关闭。我问了一个非常具体的问题,提供了我的代码、样本数据和样本数据的预期输出。如果您有
行
,其中包含文件中的所有行和entry.Value
作为要查找的单词列表,那么下面将返回包含entry.Value
中至少一个单词的所有行:
var result = lines.Where(l => l.Split(' ').Intersect(entry.Value).Any());
或更详细地介绍您的具体示例:
var result =
lines.Where(l =>
l.Trim().StartsWith("public") && entry.Value.Any(l.Contains));
您可以建立一个正则表达式,一次查找文件列表中的所有项 另外,请注意,您根本没有使用
控制器
变量
static void FindInvalidAttributes(Dictionary<string, List<string>> dictionary)
{
//Get the files from my controller dir
List<string> controllers = Directory.GetFiles(controllerPath, "*.cs", SearchOption.AllDirectories).ToList<string>();
//Iterate over my dictionary
foreach (var entry in dictionary)
{
//Build the correct file name using the dictionary key
string controller = Path.Combine(ControllerPath, entry.Key + "Controller.cs");
if (File.Exists(controller))
{
var regexText = "(?<attribLine>.*)\n" + string.Join("|", entry.Value.Select(t => "[^\n]*public\s[^\n]*" + Regex.Escape(t)))
var regex = new Regex(regexText)
//Read the file content and loop over it
var fileContent = File.ReadAllText(controller);
foreach (Match match in regex.Matches(fileContent))
{
// Here, match.Groups["attribLine"] should contain here what you're looking for.
}
}
}
}
静态void findinvalidated属性(字典)
{
//从我的控制器目录获取文件
List controllers=Directory.GetFiles(controllerPath,*.cs),SearchOption.AllDirectories.ToList();
//反复阅读我的字典
foreach(字典中的var条目)
{
//使用字典键生成正确的文件名
string controller=Path.Combine(ControllerPath,entry.Key+“controller.cs”);
if(File.Exists(controller))
{
var regexText=“(?*)\n”+string.Join(|)”,entry.Value.Select(t=>“[^\n]*public\s[^\n]*”+Regex.Escape(t)))
var regex=new regex(regexText)
//读取文件内容并在其上循环
var fileContent=File.ReadAllText(控制器);
foreach(regex.Matches中的匹配(fileContent))
{
//这里,match.Groups[“attribLine”]应该包含您要查找的内容。
}
}
}
}
我没有输入,因此无法轻松测试代码或正则表达式,但它应该给出方法和简化的总体思路。您不能使用反射来完成此任务吗?如果程序员决定这样做,方法定义可以拆分为不同的行,这将破坏您的逻辑。此外,重载方法可能会把事情搞砸。获取方法属性(相对于类属性)的一个示例是;使用反射获取方法列表作为
MethodInfo
对象,然后迭代这些对象以获取每个方法的属性。解析.cs文件我很确定这是一个大黄蜂窝。@Quantic就我的目的而言,我可以保证方法声明在一行上,并且重载不是一个问题。在我的所有控制器中定义的每个public
方法都用2个可能属性中的1个标记。一些JS文件奇怪地以非常规的方式调用这些方法,并且需要确保这些方法具有正确的属性。我将研究反射,但我更愿意解决“如何确定字符串是否包含集合中的元素”的一般问题,而不是“此方法具有哪些属性”的具体问题FindInvalidAttributes
是我的实现FindWithLINQ
是您的。第一个正确识别行中的某个内容
与myList中的某个元素匹配,第二个不匹配。我尝试了您提供的两段代码,但都没有生成任何输出。您是对的,这是因为括号。我编辑了我的回答,只是为了确保我正确地阅读了这篇文章,entry.Value。任何(l.Contains)
基本上可以理解为“如果行(l
)包含在entry.Value
中找到的任何值,正确吗?是的。正是这样means@sab669它是这个而不是,例如,IndexOf
,并且在该页上解释了注意事项。呜呜,我在处理这个问题时忘记删除控制器的声明。你能解释一下var regextextext=…
行在做什么吗?我不理解(?*)\n
并将其放入regexr.com表示?
是量词的目标。@sab669regexText
是一个正则表达式,它试图匹配一行,该行后接一行,该行至少包含列表中的一个字符串。第一行被捕获到一个命名组中-attribLine
,匹配后可以检索该组。该行包含方法的属性信息。这有意义吗?啊,我不熟悉命名组。基本上,这只是在match.Groups
集合中创建元素的一种方法,可以通过字符串引用。但是regex文本本身有一些错误:我在“[^\n]*public\s[^\n]\*”
中的s上得到了无法识别的转义序列,您可以看到(尽管直到我实际将其放入VS中,它才告诉我这是s
),OK通过在字符串前面加@
(并添加缺少的分号)。看起来不错?尽管match.Groups[“attribLine”]
似乎不太合适compile@sab669,我看到了编译问题。将foreach(var-match
更改为foreach(match-match
)。我刚刚在你的小提琴上试过,它成功了。
static void FindInvalidAttributes(Dictionary<string, List<string>> dictionary)
{
//Get the files from my controller dir
List<string> controllers = Directory.GetFiles(controllerPath, "*.cs", SearchOption.AllDirectories).ToList<string>();
//Iterate over my dictionary
foreach (var entry in dictionary)
{
//Build the correct file name using the dictionary key
string controller = Path.Combine(ControllerPath, entry.Key + "Controller.cs");
if (File.Exists(controller))
{
var regexText = "(?<attribLine>.*)\n" + string.Join("|", entry.Value.Select(t => "[^\n]*public\s[^\n]*" + Regex.Escape(t)))
var regex = new Regex(regexText)
//Read the file content and loop over it
var fileContent = File.ReadAllText(controller);
foreach (Match match in regex.Matches(fileContent))
{
// Here, match.Groups["attribLine"] should contain here what you're looking for.
}
}
}
}