好的我有多个文本文件,其中包含一些基于某些数字的文本数据。所有数据都是链接的,因此文本文件中的第一行与另一个文本文件的第一行链接,依此类推。我需要能够搜索一年(其中一个文本文件)并将与该年份相关联的所有数据输出到控制台。作为要求的一部分,文本文件必须保持独立。你会怎么推荐我这个? 这是我到目前为止所拥有的。
case 8:
Console.WriteLine("Year Month WS1_AF WS1_Rain WS1_Sun WS1_TMax WS1_Tmin");
Console.WriteLine();
Console.WriteLine();
string[][] Ws1Data = new[]
{
File.ReadAllLines(@"\A2Alg\files\Year.txt"),
File.ReadAllLines(@"\A2Alg\files\Month.txt"),
File.ReadAllLines(@"\A2Alg\files\WS1_AF.txt"),
File.ReadAllLines(@"\A2Alg\files\WS1_Rain.txt"),
File.ReadAllLines(@"\A2Alg\files\WS1_Sun.txt"),
File.ReadAllLines(@"\A2Alg\files\WS1_TMax.txt"),
File.ReadAllLines(@"\A2Alg\files\WS1_TMin.txt"),
};
int nRows = Ws1Data[0].Length;
int nColumns = Ws1Data.Length;
string tempString = " ";
for (int i = 0; i < nRows; i++)
{
tempString = " ";
for (int j = 0; j < nColumns; j++)
{
tempString = tempString + Ws1Data[j][i];
tempString = tempString + " ";
}
Console.WriteLine(tempString);
}
上面的代码读取所有文件并将其全部输出到一个数组中,它可以工作,但我需要一种方法来搜索一年,然后输出链接到那一年的数据。
答案 0 :(得分:0)
这是按年过滤的方法。
var filter = Ws1Data.Where(row => row[0] == "2016").ToArray();
或以下,如果您正在使用转置矩阵
var filter = Ws1Data.Select((row,i) => row.Where((x,j) => Ws1Data[0][j] == ("2016")).ToArray()).ToArray();
从输出代码中,这将是替换
nRows = filter[0].Length;
nColumns = filter.Length;
for (int i = 0; i < nRows; i++)
{
tempString = " ";
for (int j = 0; j < nColumns; j++)
{
tempString = tempString + filter[j][i];
tempString = tempString + " ";
}
Console.WriteLine(tempString);
}
这是一个完整的演示
string [][] Ws1Data = new string[3][];
Ws1Data[0] = new string[3] { "2015", "2016", "2016" };
Ws1Data[1] = new string[3] { "5asda", "6asda","7asa" };
Ws1Data[2] = new string[3] { "5sdfdf", "6asda","6dsfsdf" };
var filter = Ws1Data.Select((row,i) => row.Where((x,j) => Ws1Data[0][j] == ("2016")).ToArray()).ToArray();
int nRows = Ws1Data[0].Length;
int nColumns = Ws1Data.Length;
string tempString = " ";
for (int i = 0; i < nRows; i++)
{
tempString = " ";
for (int j = 0; j < nColumns; j++)
{
tempString = tempString + Ws1Data[j][i];
tempString = tempString + " ";
}
Console.WriteLine(tempString);
}
Console.WriteLine("Filtered by 2016");
nRows = filter[0].Length;
nColumns = filter.Length;
for (int i = 0; i < nRows; i++)
{
tempString = " ";
for (int j = 0; j < nColumns; j++)
{
tempString = tempString + filter[j][i];
tempString = tempString + " ";
}
Console.WriteLine(tempString);
}
Console.ReadLine();