简体   繁体   中英

How to filter a Tuple by a single item C#

I am creating an application that displays a Tuple as a table in the command line and need to be able to filter this by item 1. My current implementation of displaying the Tuple is:

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(" ------------------------------------------------------------------------------------------------");}

etc

Now what I need to do is I need the be able to filter this table so that for example, filtering item1 to include rows with values on "Monday". This is how I have attempted to implement the filter:

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));

When attempting to compile the current file I get this error:

'System.Collections.Generic.IEnumerable<System.Tuple<string,string,string,string,string,string>>' does not contain a definition for 'ForEach' and no extension method 'ForEach' accepting a first argument of type 'System.Collections.Generic.IEnumerable<System.Tuple<string,string,string,string,string,string>>' could be found (are you missing a using directive or an assembly reference?)

List<T> implements IEnumerable<T> . So when you are reassigning daySearch it compiles. You should introduce a daySearchList and use that.

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

Or just drop your first query, since it was just getting overwritten.

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

In your declaration of daySearch, you used var daySearch , which results in an IEnumerable because you don't assign a List to it, just the IEnumerable result of the .Where() . Use something like:

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

This should allow you to use the .ForEach extension method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM