-3

I have a csv with 2 columns the first one with a string and the second with the start and end time (ex: 11:00-22:00). How would you filter the fields containing the time of 14:00 for example, as performatively as possible?

For example :

RestaurantName OpenHours

Kushi Tsuru 11:30-21:00

Osakaya Restaurant 11:30-21:00

The Stinking Rose 9:00-22:00

I write 21:30 and would show The Stinking Rose, or if i write 11:00 show Kushi Tsuru, Osakaya Restaurant and The Stinking Rose on console.

class Filter
{
    static void Main(string[] args)
    {
        List<Restaurant> csvFile = File.ReadAllLines(@"C:\restaurant-hours.csv")
                                   .Skip(1)
                                   .Select(Restaurant.FromCsv)
                                   .ToList();

        void filter_by_hour()
        {
            Console.WriteLine("\nEnter the hour");
            string Dateofbirth = (Console.ReadLine();
            var filter_data = csvFile.Where(e => (DateTime.Parse(e.OpenHour)) > Dateofbirth)
                              .Select(e => e);
        }

    }
}
Thiago Cruz
  • 75
  • 1
  • 9
  • 1
    You could load the file into a DataTable then filter it. https://stackoverflow.com/questions/20860101/how-to-read-text-file-to-datatable – jac Oct 10 '19 at 00:39
  • @Jac why would you use a DataTable? Why not create objects to represent the data? – mason Oct 10 '19 at 00:59
  • Could you please [edit] post to show your current non-performant implementation as well your current/expected performance goals? – Alexei Levenkov Oct 10 '19 at 00:59
  • Yeah, i`ve filtered by ":", but the important is "I write 21:30 and would show The Stinking Rose, or if i write 11:00 show Kushi Tsuru, Osakaya Restaurant and The Stinking Rose on console." – Thiago Cruz Oct 10 '19 at 01:57
  • The code `csvFile.Where(e => (DateTime.Parse(e.OpenHour)) > Dateofbirth)` does not seem to check if value falls in range (and should not even compile as `DateTime` is not comparable to a string). Are you sure you've posted correct code? Plus provide results of your performance measurements/goals so actual "do … as performatively as possible" question can be answered in practical way. Also clarify whether performance is important for one-time operation or concern is about repeated calls to filter. (Thanks for edit to provide at least partial information in the question) – Alexei Levenkov Oct 10 '19 at 02:15
  • Smells like a homework request. You've posted no working code showing what you've done. You've basically asked "someone do this for me". When someone posted an answer, you complained that it didn't properly validate their input. Voting to close as off-topic. – oscilatingcretin Oct 10 '19 at 14:02
  • @mason you could add objects. I think that would be more work to filter the data than a quick import into a DataTable where filtering sorting, etc. are already available. Plus this has been my go to in the past. You tend to do what you know. – jac Oct 10 '19 at 23:25
  • @Jac it's far easier to filter data on objects with Linq, and less overhead. DataTable should rarely be used for anything. – mason Oct 11 '19 at 01:48

1 Answers1

0

This is faster than LINQ and Lambda functions. Also, many variables can be deleted, just added them for clarity. Already tested, it works.

    static void Main(string[] args)
        {
            StreamReader reader = new StreamReader("a.txt");
            string[] time = Console.ReadLine().Split(':');
            int hour = 0;
            int minute = 0;

            try
            {
               hour = Convert.ToInt32(time[0]);
               minute = Convert.ToInt32(time[1]); 
            }
            catch
            {
               Console.WriteLine("The input must be something like aa:bb, with aa > 00 and aa < 24, bb >= 00 and bb <=53");
               return;
            }

            string[] first_time;
            string[] second_time;
            string[] time_frame;

            int first_hour;
            int first_minute;
            int second_hour;
            int second_minute;

            DateTime _date = new DateTime(1, 1, 1, hour, minute, 0);
            DateTime first_date, second_date;

            string line = "";
            string[] tokens;
            while ((line = reader.ReadLine()) != null)
            {
                tokens = line.Split(' ');
                time_frame = tokens[tokens.Length - 1].Split('-');
                first_time = time_frame[0].Split(':');
                first_hour = Convert.ToInt32(first_time[0]);
                first_minute = Convert.ToInt32(first_time[1]);

                first_date = new DateTime(1, 1, 1, first_hour, first_minute, 0);
                second_time = time_frame[1].Split(':');
                second_hour = Convert.ToInt32(second_time[0]);
                second_minute = Convert.ToInt32(second_time[1]);

                second_date = new DateTime(1, 1, 1, second_hour, second_minute, 0);

                if (_date >= first_date && _date <= second_date)
                {
                    for (int i = 0; i < tokens.Length - 1; i++)
                    {
                        Console.Write(tokens[i]);
                        Console.Write(" ");
                    }
                    Console.WriteLine("");
                }
            }    
            reader.Close();
        }
Javier Silva Ortíz
  • 2,864
  • 1
  • 12
  • 21