如何通过单个项目C#过滤元组



我正在创建一个应用程序,该应用程序在命令行中将Tuple显示为表,并且需要能够按项目1进行筛选。我目前显示元组的实现是:

Console.Clear(); //clears the console
var file1 = @"FilesDay.txt"; //location of the Day file
var file2 = @"FilesDate.txt"; //location of the Date file
var file3 = @"FilesValue1.txt"; //location of the value1
var file4 = @"FilesValue2.txt"; //location of the value2
var file5 = @"FilesValue3.txt"; //location of the value3
var file6 = @"FilesValue4.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(" ------------------------------------------------------------------------------------------------");}

etc

现在我需要做的是,我需要能够过滤这个表,例如,过滤item1以包括在"Monday"上具有值的行。这就是我尝试实现过滤器的方式:

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"的定义,也不包含扩展方法"ForEah"接受类型的第一个参数可以找到'System.Collections.Generic.IEnumerable<System.Tuple<string,string,string,string,string,string>>'(您是否缺少using指令或程序集引用?)

List<T>实现IEnumerable<T>。因此,当您重新分配daySearch时,它会编译。您应该引入一个daySearchList并使用它。

var daySearch = Content.Where(tuple => tuple.Item1 == DaySearchInput);
var daySearchList = Content.OrderByDescending(item => item.Item2).ToList();

或者只是删除您的第一个查询,因为它刚刚被覆盖。

var daySearch = Content.OrderByDescending(item => item.Item2).ToList();

在您的daySearch声明中,您使用了var daySearch,这会产生一个IEnumerable,因为您没有为它分配List,只分配.Where()的IEnumeraable结果。使用类似的东西

var daySearch = Content.Where(tuple => tuple.Item1 == DaySearchInput)
                 .OrderByDescending(item => item.Item2).ToList();

这应该允许您使用.ForEach扩展方法。

最新更新