在 C# 中使用时间跨度简化 if else 条件 [英] Simplify if else condition using timespan in C#
问题描述
我必须创建实时报告.为此,我必须为给定日期的每个小时编写条件.在下面的代码中,条件检查当前星期几,然后检查当前时间,并在此基础上生成报告.
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.
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
}
}
上面的代码仅适用于星期一,我也必须对一周的其他六天做同样的事情.当我在每个条件中添加查询时,它就像数百行.
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.
是否有更好的方法来完成这项工作,而无需编写数百行代码?
Is there a better way to get this done without having to write hundreds of lines of code?
推荐答案
这对您有用吗?
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
那么你可以使用这个:
If you want DayOfWeek
and Hour
then you could use this:
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];
<小时>
根据评论和其他答案,这里有一个更简洁的方法:
Based on the comments and other answers, here's a more succinct way of doing it:
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# 中使用时间跨度简化 if else 条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!