> 在 C# 中,c#,list,dictionary,if-statement,npoco"/>

首页 > 解决方案 > 如何在 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;                               
}

标签: c#listdictionaryif-statementnpoco

解决方案


您需要一个日期时间对象进行比较:

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);

推荐阅读