数据表按确切的DateTime选择 [英] DataTable Select by exact DateTime

查看:61
本文介绍了数据表按确切的DateTime选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据表 xData ,其中的 DateTime 列具有毫秒精度,我需要提取其中的行精确匹配时间(以毫秒为单位),但语法不正确。

I have a DataTable xData which has a DateTime column with millisecond accuracy, I need to extract the rows which match an exact time with milliseconds but cannot get the syntax correct.

我尝试过

DateTime dt = timeStampList[i];
string str = dt.ToString("yyyy-MM-dd HH:mm:ss.fff");
DataRow[] result = xData.Select("TimeStamp2 = " + str);

foreach (DataRow row in result)
{
    Console.WriteLine("{0}, {1}", row[0], row[1]);
}

DataRow[] result = xData.Select("TimeStamp2 = " + str);

导致错误:


语法错误:'16'运算符后缺少操作数。

Syntax error: Missing operand after '16' operator.

我已经搜索过,但大多数示例仅显示如何选择日期而不是完整的 datetime (以毫秒为单位)。

I've searched but most of the examples only show how to select by date and not a full datetime with milliseconds.

推荐答案

问题在于您必须将要查找的日期/时间传递给过滤器。

The problem is that you have to pass the date/time you're looking for to your filter.

起初,我认为选择,但是根据德米特里的回答。

At first I thought that sub second precision was not supported by Select but according to Dmitry's answer it is.

另一种方法是使用对数据集的查询,它可以避免序列化日期/时间:

Another approach is to use Linq to DataSet which allows you to avoid serializing the date/time:

DateTime dt = timeStampList[i];

IEnumerable<DataRow> selectedRows = 
  xData.AsEnumerable().Where(row => (row.Field<DateTime>("TimeStamp2") == dt));

foreach (DataRow row in selectedRows)
{
  Console.WriteLine("{0}, {1}", row[0], row[1]);
}

这篇关于数据表按确切的DateTime选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆