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

前端之家收集整理的这篇文章主要介绍了使用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#相关文章