我已經寫了一個函數,它給了我一個匹配多維正則表達式字符串的多維數組。 (FileCheck [] [])LINQ查詢找到多數重複的多維數組字符串
- FileCheck [0] //這串[]包含所有的文件名
- FileCheck [1] //這串[]是依賴於正則表達式匹配是0或1找到。
FileCheck [2] //此字符串[]包含找到的第一個正則表達式的索引。
foreach (string File in InputFolder) { int j = 0; FileCheck[0][k] = Path.GetFileName(File); Console.WriteLine(FileCheck[0][k]); foreach (Regex Filemask in Filemasks) { if (string.IsNullOrEmpty(FileCheck[1][k]) || FileCheck[1][k] == "0") { if (Filemask.IsMatch(FileCheck[0][k])) { FileCheck[1][k] = "1"; FileCheck[2][k] = j.ToString(); // This is the Index of the Regex thats Valid } else { FileCheck[1][k] = "0"; } j++; } Console.WriteLine(FileCheck[1][k]); } k++; } Console.ReadLine(); // I need the Index of the Regex with the most valid hits
我試圖寫一個函數,讓我擁有最重複的RegexIndex串。 這是我試過,但沒有工作:((我只得到字符串的計數最複製而不是字符串本身)
// I need the Index of the Regex with the most valid hits
var LINQ = Enumerable.Range(0, FileCheck[0].GetLength(0))
.Where(x => FileCheck[1][x] == "1")
.GroupBy(x => FileCheck[2][x])
.OrderByDescending(x => x.Count())
.First().ToList();
Console.WriteLine(LINQ[1]);
示例數據
string[][] FileCheck = new string[3][];
FileCheck[0] = new string[]{ "1.csv", "TestValid1.txt", "TestValid2.txt", "2.xml", "TestAlsoValid.xml", "TestValid3.txt"};
FileCheck[1] = new string[]{ "0","1","1","0","1","1"};
FileCheck[2] = new string[]{ null, "3", "3", null,"1","2"};
在這個例子中我需要爲LINQ查詢的結果是:
string result = "3";
什麼意思是「但沒用」?你得到的結果是什麼?例外?任何不同的結果?請對你的問題更具體。 – HimBromBeere
我得到大多數元素的計數,但不是RegexIndex的字符串。我編輯了我的問題,使其更清楚。 – kami
@kami嗯,你確定?你上面的查詢給出了'2',這是字符串,而不是count(它是3) –