![](/img/trans.png)
[英]How to filter a list of tuples based on matching one item of the tuple to another list through lambda expression in C#?
[英]How to filter a Tuple by a single item C#
我正在创建一个应用程序,该应用程序在命令行中将元组显示为表格,并且需要能够按项目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>>'
的第一个参数'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()
的IEnumerable结果。 使用类似:
var daySearch = Content.Where(tuple => tuple.Item1 == DaySearchInput)
.OrderByDescending(item => item.Item2).ToList();
这应该允许您使用.ForEach
扩展方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.