I have written a function that gives me an multidimensional array of an Match with multiple regex strings. (FileCheck[][])
- FileCheck[0] // This string[] contains all the filenames
- FileCheck[1] // This string[] is 0 or 1 depending on a Regex match is found.
FileCheck[2] // This string[] contains the Index of the first found Regex.
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
I'm trying to write a function that gives me the string of the RegexIndex that has the most duplicates. This is what I tried but did not work :( (I only get the count of the string the the most duplicates but not the string itself)
// 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]);
Example Data
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"};
In this example I need as result of the Linq query:
string result = "3";
2which is the string, not the count (which is 3)