c# - 如何在 Dictionary 中的日期时间上使用 IF 语句> 在 C# 中
问题描述
我从数据库中提取的数据是(字符串,日期时间),我需要查看日期时间以查看它是否等于今天 @ 14:00 的 sysdate,但我无法弄清楚如何只查看列表中的日期时间字段。
int recordCount;
using (IDatabase db = SystemStatus.GetDatabase(_connectionStrings.stuff))
{
const string query= @"
SELECT DISTINCT string, date
FROM table
WHERE date > DATE_SUB(SYSDATE(), Interval 30 day) AND
drop_date < DATE_SUB(NOW(), Interval 12 hour) AND
status_id = 2;";
var records = db.Fetch<Dictionary<string, List<DateTime>>>(query);
recordCount = records.Count;
}
解决方案
您需要一个日期时间对象进行比较:
DateTime now = DateTime.Now;
DateTime dt = new DateTime(now.Year, now.Month, now.Day, 14, 0, 0); // 2 PM today
您不需要“if”语句。您需要使用对象查询(LINQ)...
using System.Linq;
// get everything past 2 PM today
var after2PMRecords = records.Where(r => r.Value > dt);
// get everything before 2 PM today
var before2PMRecords = records.Where(r => r.Value < dt);
推荐阅读
- python - 如何运行每5分钟执行一次但同时执行其他一些脚本的python脚本
- python - 使用 NP where 给出“具有多个元素的数组的真值不明确”错误
- amazon-web-services - AWS 公共 DNS 无法访问
- java - 如何使用数字逻辑打印以下模式?
- ckan - 查找上传到 CKAN 的总数据大小
- r - DT SearchPanes 的问题:窗格中仅出现前 10 个唯一值
- node.js - 如何通过 Firebase Cloud Function 触发 GCP Google Speech API?
- python - 每次捕获一个重复捕获组
- oracle - 重复行 - 交互式网格 - Apex Oracle
- sql - 如何选择满足或超过某些标准的第一个值?