I could swear this was working two days ago, now it throws an exception...
I am checking against some data in a DataTable
. I'm basically counting how many times a certain eventID
is found within the last 15 minutes. 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:
DateTime now = DateTime.Now.AddMinutes(-15);
However this throws a String was not recognized as a valid DateTime exception. Here is an example of the data in the datatable, in the column for RHDateEvent:
2017-02-14 13:58:27 PM (edit - yes this is only one date, not two, in the column)
So what am I doing wrong? Do I need to be converting this DateTime somehow?
I'd really recommend to use Linq-To-DataTable
instead of the old and limited Select
method:
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.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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.