在C#中使用timespan简化if else条件

prk*_*ash 6 c# timespan

我必须创建一个实时报告.为此,我必须为某一天的每一小时写下条件.在下面的代码中,条件检查当前星期几,然后检查当前时间并根据必须生成报告.

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 >= sixthHour && DateTime.Now.TimeOfDay <= 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 >= seventhHour && DateTime.Now.TimeOfDay <= eigthHour)
        {

        }
        else if (DateTime.Now.TimeOfDay >= eigthHour && DateTime.Now.TimeOfDay <= ninthHour)
        {

        }
        else if (DateTime.Now.TimeOfDay >= ninthHour && DateTime.Now.TimeOfDay <= tenthHour)
        {

        }
        else if (DateTime.Now.TimeOfDay >= tenthHour && DateTime.Now.TimeOfDay <= eleventhHour)
        {

        }
        // and so on for the entire cycle of time
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的代码仅适用于星期一,我也必须为一周中的其他六天做同样的事情.当我在每个条件中添加查询时,它就像数百行.

有没有更好的方法来完成这项工作而无需编写数百行代码?

Eni*_*ity 10

这对你有用吗?

var sqls = new []
{
    "select x from y",
    "select w from q",
    // etc - 24 options
};

var sql = sqls[DateTime.Now.Hour];
Run Code Online (Sandbox Code Playgroud)

甚至:

var sqls = new Action[]
{
    () => { /* sql for midnight */ },
    () => { /* sql for 1 am */ },
    // etc
    () => { /* sql for 11 pm */ },
};

var sql = sqls[DateTime.Now.Hour];

sql.Invoke();
Run Code Online (Sandbox Code Playgroud)

如果你想DayOfWeek,Hour然后你可以使用这个:

var sqls = new string[][]
{
    new [] { "select x from y", "select w from q", },
    new [] { "select x from y", "select w from q", },
    new [] { "select x from y", "select w from q", },
    new [] { "select x from y", "select w from q", },
    new [] { "select x from y", "select w from q", },
    new [] { "select x from y", "select w from q", },
    new [] { "select x from y", "select w from q", },
};

var sql = sqls[(int)DateTime.Now.DayOfWeek][DateTime.Now.Hour];
Run Code Online (Sandbox Code Playgroud)

根据评论和其他答案,这里有一个更简洁的方法:

string day = DateTime.Now.DayOfWeek.ToString().Substring(0, 3);

string[] shifts = new []
{
    "('22:00-7:00')",
    "('22:00-7:00', '6:00-15:00')",
    // 24
};

string shift = shifts[DateTime.Now.Hour];

string sql = $"SELECT agentlogin FROM agentdetails WHERE location = 'PNQ10-Pune' AND shift IN {shifts} AND {day} = 'W'";
Run Code Online (Sandbox Code Playgroud)