title: Simplify if else condition using timespan in C#
body: <p>I have to create a real time report. For that, I have to write conditions for each and every hour of a given day. In the code below, the condition checks for the current day of week and then check for the current time and based on that a report has to be generated.</p>
  
  <pre><code>protected void sample()
  {
      TimeSpan zerothHour = new TimeSpan(00, 0, 0);
      TimeSpan firstHour = new TimeSpan(01, 0, 0);
      TimeSpan secondHour = new TimeSpan(02, 0, 0);
      TimeSpan thirdHour = new TimeSpan(03, 0, 0);
      TimeSpan fourthHour = new TimeSpan(04, 0, 0);
      TimeSpan fifthHour = new TimeSpan(05, 0, 0);
      TimeSpan sixthHour = new TimeSpan(06, 0, 0); 
      // and so on until the twentyfourth hour
      if (DateTime.Today.DayOfWeek == DayOfWeek.Monday)
      {
          if (DateTime.Now.TimeOfDay &gt;= sixthHour &amp;&amp; DateTime.Now.TimeOfDay &lt;= seventhHour)
          {
              //MySql query here
              string MyConString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
              MySqlConnection connection = new MySqlConnection(MyConString);
              string agentlogin = "SELECT agentlogin FROM agentdetails WHERE location = 'PNQ10-Pune' AND shift IN('6:00-15-00', '22:00-7:00') AND Mon = 'W'";
              MySqlCommand cmd = new MySqlCommand(agentlogin, connection);
              connection.Open();
              MySqlDataReader rdr = cmd.ExecuteReader();
              while (rdr.Read())
              {
                 //lblagentlogin.Text += rdr["agentlogin"] + Environment.NewLine;
                  sqlList.Add(Convert.ToString(rdr["agentlogin"]));
              }
          }
          else if(DateTime.Now.TimeOfDay &gt;= seventhHour &amp;&amp; DateTime.Now.TimeOfDay &lt;= eigthHour)
          {
  
          }
          else if (DateTime.Now.TimeOfDay &gt;= eigthHour &amp;&amp; DateTime.Now.TimeOfDay &lt;= ninthHour)
          {
  
          }
          else if (DateTime.Now.TimeOfDay &gt;= ninthHour &amp;&amp; DateTime.Now.TimeOfDay &lt;= tenthHour)
          {
  
          }
          else if (DateTime.Now.TimeOfDay &gt;= tenthHour &amp;&amp; DateTime.Now.TimeOfDay &lt;= eleventhHour)
          {
  
          }
          // and so on for the entire cycle of time
      }
  }
  </code></pre>
  
  <p>The code above is only for Monday and I have to do the same thing for the other six days of week too. When I add the queries inside each conditions, it would be like hundreds of lines.</p>
  
  <p>Is there a better way to get this done without having to write hundreds of lines of code?</p>