Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 选择Many枚举按字符串值筛选的多个文件需要索引_C#_Linq - Fatal编程技术网

C# 选择Many枚举按字符串值筛选的多个文件需要索引

C# 选择Many枚举按字符串值筛选的多个文件需要索引,c#,linq,C#,Linq,我有一个C#LINQ查询,它接收许多文件并枚举每个文件以查找“危险字符串”。当每个文件找到“危险字符串”时,我需要将文件名作为键保存到字典中,并将找到的行号作为数组保存到字典值中(每个文件将多次出现找到的“危险字符串”)。我似乎不知道如何从查询中获取行号。目前,它所做的只是从0开始,在每次找到字符串时进行计数,并保存到数组中,而不是实际的行号本身 救命啊 电流输出: Namespace risk strings were found on the below classes, please re

我有一个C#LINQ查询,它接收许多文件并枚举每个文件以查找“危险字符串”。当每个文件找到“危险字符串”时,我需要将文件名作为键保存到字典中,并将找到的行号作为数组保存到字典值中(每个文件将多次出现找到的“危险字符串”)。我似乎不知道如何从查询中获取行号。目前,它所做的只是从0开始,在每次找到字符串时进行计数,并保存到数组中,而不是实际的行号本身

救命啊

电流输出:

Namespace risk strings were found on the below classes, please review the results.
..\..\src\classes\BookingCls.cls risk found on lines:  0,  1,  2, 
..\..\src\classes\BookingDaySnapshotCls.cls risk found on lines:  3, 
..\..\src\classes\BookingEventCls.cls risk found on lines:  4,  5, 
..\..\src\classes\BookingOwnerChangeExt.cls risk found on lines:  6,  7,  8, 
..\..\src\classes\BookingSourceChangeExt.cls risk found on lines:  9,  10,  11, 
..\..\src\classes\GuestroomTypeDayCls.cls risk found on lines:  12, 

我会在加载文件时指定行号

var files = Directory.EnumerateFiles(srcPath, "*", SearchOption.AllDirectories)
    .Where(s => s.EndsWith(riskFileType))
    .SelectMany(
        file => File.ReadLines(file).Select((line, index) => new {line, rowNumber = index + 1}), 
        (file, line) => new {file, line.line, line.rowNumber})
    .Where(@t => filterStringExpression.IsMatch(t.line))
    .GroupBy(q => q.file)
    .ToDictionary(g => g.Key, g => g.Select(q => q.rowNumber))
    .ToArray();
这只是另一种查询:

var files = Directory.EnumerateFiles(srcPath, "*", SearchOption.AllDirectories)
    .Where(s => s.EndsWith(riskFileType))
    .SelectMany(
        file => File.ReadLines(file).Select((line, index) => new {line, rowNumber = index + 1}), 
        (file, line) => new {file, line.line, line.rowNumber})
    .Where(@t => filterStringExpression.IsMatch(t.line))
    .ToLookup(k => k.file, e => e.rowNumber);

我喜欢你的想法,但我不明白
Distinct
的功能是什么。您有一个(文件、行、行)元组序列;当然这些都是不同的,对吗?你永远不会有两个相同的文件,同一行,在同一个位置。你我的朋友是一个绅士和学者!我一整天都在忙这个!我是LINQ的新手,现在仍在学习诀窍,并不是100%确定distinct做了什么。既然你解释了这更有意义。一切都很好谢谢你@埃里克·利珀特——你说得对,谢谢。查询已修复。我建议将其作为LINQ学习工具
var files = Directory.EnumerateFiles(srcPath, "*", SearchOption.AllDirectories)
    .Where(s => s.EndsWith(riskFileType))
    .SelectMany(
        file => File.ReadLines(file).Select((line, index) => new {line, rowNumber = index + 1}), 
        (file, line) => new {file, line.line, line.rowNumber})
    .Where(@t => filterStringExpression.IsMatch(t.line))
    .ToLookup(k => k.file, e => e.rowNumber);