【发布时间】:2015-03-26 04:17:37
【问题描述】:
我正在创建一个在命令行中将元组显示为表格的应用程序,并且需要能够按第 1 项对其进行过滤。我当前显示元组的实现是:
Console.Clear(); //clears the console
var file1 = @"Files\Day.txt"; //location of the Day file
var file2 = @"Files\Date.txt"; //location of the Date file
var file3 = @"Files\Value1.txt"; //location of the value1
var file4 = @"Files\Value2.txt"; //location of the value2
var file5 = @"Files\Value3.txt"; //location of the value3
var file6 = @"Files\Value4.txt"; //location of the value4
var days = System.IO.File.ReadAllLines(file1);
var dates = System.IO.File.ReadAllLines(file2);
var value1 = System.IO.File.ReadAllLines(file3);
var value2 = System.IO.File.ReadAllLines(file4);
var value3 = System.IO.File.ReadAllLines(file5);
var value4 = System.IO.File.ReadAllLines(file6);
var columnCount = days.Length;
var Content = new List<Tuple<string, string, string, string, string, string>>(); // The list of items read from each file
for (int i = 0; i < columnCount; i++) // Add a new item for each line in each file
{
Content.Add(new Tuple<string, string, string, string, string, string>(days[i], dates[i], value1[i], value2[i], value3[i], value4[i]));
}
Console.Clear();
Console.WriteLine(" - ASCENDING ORDER -----------------------------------------------------------------------------");
Console.WriteLine("| DAY DATE VALUE1 VALUE2 VALUE3 | VALUE4 |");
Console.WriteLine(" -----------------------------------------------------------------------------------------------");
Content = Content.OrderBy(item => item.Item2).ToList();
Content.ForEach(item => Console.WriteLine(" {0} \t {1} \t {2} \t {3} \t {4} \t {5}", item.Item1, item.Item2, item.Item3, item.Item4, item.Item5, item.Item6));
Console.WriteLine(" ------------------------------------------------------------------------------------------------");}
等
现在我需要做的是我需要能够过滤此表,例如,过滤 item1 以包含具有“星期一”值的行。这就是我尝试实现过滤器的方式:
string DaySearchInput;
Console.WriteLine("\nYou may search for items on the following days;");
Console.Write("\nPlease enter the day of the week you wish to search for (case sensitive): ");
DaySearchInput= Console.ReadLine();
var daySearch = Content.Where(tuple => tuple.Item1 == DaySearchInput);
daySearch = Content.OrderByDescending(item => item.Item2).ToList();
Console.Clear();
Console.WriteLine("\n - VALUES ON A {0} --------------------------------------------------------------------", DaySearchInput);
Console.WriteLine("| DAY DATE VALUE1 VALUE2 VALUE3 | VALUE4 |");
Console.WriteLine(" -----------------------------------------------------------------------------------------------");
daySearch.ForEach(item => Console.WriteLine(" {0} \t {1} \t {2} \t {3} \t {4} \t {5}", item.Item1, item.Item2, item.Item3, item.Item4, item.Item5, item.Item6));
尝试编译当前文件时出现此错误:
'System.Collections.Generic.IEnumerable<System.Tuple<string,string,string,string,string,string>>' 不包含“ForEach”的定义,也没有扩展方法“ForEach”
接受类型的第一个参数
可以找到'System.Collections.Generic.IEnumerable<System.Tuple<string,string,string,string,string,string>>'(您是否缺少 using 指令或程序集引用?)
【问题讨论】: