![](/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.