[英]Issue with DateTime
I could swear this was working two days ago, now it throws an exception... 我可以发誓这是两天前的工作,现在它引发了异常...
I am checking against some data in a DataTable
. 我正在对照
DataTable
中的某些数据进行检查。 I'm basically counting how many times a certain eventID
is found within the last 15 minutes. 我基本上是在计算过去15分钟内找到某个
eventID
次数。 Here's that code: 这是代码:
int startevents = trackingData
.Select("RHEventID = 3 AND RHDateEvent > #" + now + "#" ).Length;
I'm defining the 'now' variable just before that - looks like this: 我在此之前定义'now'变量-看起来像这样:
DateTime now = DateTime.Now.AddMinutes(-15);
However this throws a String was not recognized as a valid DateTime exception. 但是,此操作引发的字符串未被识别为有效的DateTime异常。 Here is an example of the data in the datatable, in the column for RHDateEvent:
这是RHDateEvent列中数据表中数据的示例:
2017-02-14 13:58:27 PM (edit - yes this is only one date, not two, in the column)
2017-02-14 13:58:27 PM(编辑-是的,这只是该栏中的一个日期,而不是两个日期)
So what am I doing wrong? 那我在做什么错? Do I need to be converting this DateTime somehow?
我是否需要以某种方式转换此DateTime?
I'd really recommend to use Linq-To-DataTable
instead of the old and limited Select
method: 我真的建议使用
Linq-To-DataTable
代替旧的有限Select
方法:
DateTime in15minutes = DateTime.Now.AddMinutes(15);
var matchingRows = from row in trackingData.AsEnumerable()
where row.Field<int>("RHEventID) == 3
&& row.Field<DateTime>("RHDateEvent") > in15minutes
select row;
if you now just need the count use: 如果您现在只需要计数,请使用:
int matchingRowCount = matchingRows.Count();
This is more readable, powerful and supports compile time safety. 这更具可读性,功能强大并支持编译时安全。
If your column is a not a DateTime
- but a string
-column you need to parse it: 如果您的列不是
DateTime
而是string
列,则需要对其进行解析:
...
&& DateTime.Parse(row.Field<string>("RHDateEvent"), CultureInfo.InvariantCulture) > in15minutes
It looks like the date time is duplicated... 看来日期时间重复了...
2017-02-14 13:58:27 PM 2017-02-14 13:57:27 PM
instead of 代替
2017-02-14 13:58:27 PM
如果是实体使用的限制
var startevents = trackingData.Where(e => e.RHEventId = 3 && e.RHDateEvent >= DateTime.Now.AddMinutes(-15)).Count();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.