Zoltan
Zoltan

Reputation: 63

c# how to count in datatable between specified time

I have a data table, and would like to count the NR 1 is how many times was registered between Time 6-14:

example:

NAME   FABRIKNR   DATE           TIME
abc     1       24.04.2017    06:00:00
abc     2       24.04.2017    08:00:00
abc     3       24.04.2017    10:00:00
abc     1       24.04.2017    11:00:00
abc     2       24.04.2017    11:10:00
abc     3       24.04.2017    14:20:00
abc     1       24.04.2017    19:00:00
abc     2       24.04.2017    22:00:00
abc     3       24.04.2017    22:00:00

I started to calculate with the Date.Time seconds and with break times but i think it is a wrong way.

I can count the Fabrik Nr 1 and it works:

  foreach (string text_line in raw_text)
        {


            data_col = text_line.Split(';');

            //header
            if (x == 0)
            {
                for (int i = 0; i <= data_col.Count() - 1; i++)
                {
                    dgv_table.Columns.Add(data_col[i]);
                }
                x++;
            }

            else
            {
                //data
                dgv_table.Rows.Add(data_col);
            }
            // timer1.Start();
        }

        DataRow[] rowCount = dgv_table.Select("FABRIKNR = '1'");
        this.lb_qty.Text = rowCount.Length.ToString();

        NrOfCycles = lb_qty.Text;

I think i have to check the time range DataRow[] rowCount = dgv_table.Select("FABRIKNR = '1' AND TIME >= '06:00:00' AND TIME < '14:00:00'");

But something is wrong the program stops on reading and don't give any error(no freeze).

Can anyone help me in search? And the result is... :) pabammm...

  DataRow[] rowCount = dgv_table.Select("FABRIKNR = '1' AND (TIME >= '6:00:00')and (TIME < '14:00:00') ");
   DataRow[] rowCount = dgv_table.Select("FABRIKNR = '1' AND (TIME >= '14:00:00')and (TIME < '22:00:00') ");

Upvotes: 1

Views: 154

Answers (2)

Joshua Hysong
Joshua Hysong

Reputation: 1072

You have a typo in your select query. Change it to the following:

DataRow[] rowCount = dgv_table.Select("FABRIKNR = '1' AND TIME >= '06:00:00' AND TIME < '14:00:00'");

TIME and AND where flipped. Also, TIME should be >= or > than your starting time and < or <= to your ending time.

Upvotes: 1

Stephan Bauer
Stephan Bauer

Reputation: 9249

There's another typo, it should be >= instead of =>:

DataRow[] rowCount = dgv_table.Select("FABRIKNR = '1' AND TIME >= '06:00:00'  AND TIME < '14:00:00'");

Upvotes: 1

Related Questions