使用c#中的timespan简化if else条件

发布时间:2019-03-14 整理:脚本之家 作者:未知
脚本之家收集整理的这篇文章主要介绍了使用c#中的timespan简化if else条件脚本之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随脚本之家小编过来看看吧!
我必须创建一个实时报告,为此我必须为某一天的每一小时写下条件.在下面的代码中,条件检查当前星期几,然后检查当前时间并根据必须生成报告.

以下是代码:

protected void sample()
    {
        TimeSpan zerothHour = new TimeSpan(00,0);
        TimeSpan firstHour = new TimeSpan(01,0);
        TimeSpan secondHour = new TimeSpan(02,0);
        TimeSpan thirdHour = new TimeSpan(03,0);
        TimeSpan fourthHour = new TimeSpan(04,0);
        TimeSpan fifthHour = new TimeSpan(05,0);
        TimeSpan sixthHour = new TimeSpan(06,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
        }
    }

上面的代码仅适用于星期一,我也必须在一周的其他六天内做同样的事情.当我在每个条件中添加查询时,它将像数百行.所以我有一个更好的方法来完成这项工作,而无需编写数百行代码?请告诉我.提前致谢.

这对你有用吗?
var sqls = new []
{
    "select x from y","select w from q",// etc - 24 options
};

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

甚至:

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

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

sql.Invoke();

如果您想要DayOfWeek和Hour,那么您可以使用:

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

var sql = sqls[(int)DateTime.Now.DayOfWeek][DateTime.Now.Hour];

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

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'";

总结

以上是脚本之家为你收集整理的使用c#中的timespan简化if else条件全部内容,希望文章能够帮你解决使用c#中的timespan简化if else条件所遇到的程序开发问题。

如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。

标签:timespan