ADO.NET: query a DataTable on a timeout
I have an in-memory DataTable that contains two fields of type DateTime
, respectively LastAction and LastHeartbeat meaning the last time an entity sent an activity log message and the last time the entity sent a heartbeat ("I'm alive") log message.
I'm building a method to query the data table with various criteria, including the "max开发者_如何学Goimum inactivity time" of an entity in the system. This maximum timeout is meant in order to consider both LastAction and LastHeartbeat, so the query will resemble something like (LastAction > Now-timeout || LastHeartbeat > Now-timeout)
and that's OK for now. The timeout value is expressed in integer seconds.
Currently I do this in two lines
DateTime lastActivity = DateTime.UtcNow.Subtract(TimeSpan.FromSeconds(maxinactivity));
filters.Add(string.Format("({0} >= {2} OR {1} >= {2}}", _colLastAction.ColumnName, _colLastHeartbeat.ColumnName, lastActivity.ToString(CultureInfo.InvariantCulture)));
where the final result will look like (LastAction >= #...# OR LastHeartbeat >= #...#)
depending on the current time the query is executed
My question is: can I do date subtraction from within the query? Do operators exist so I can directly type the timeout inside the query? In SQL, at least with MySQL, I was able to do date subtraction with the NOW() function. Is it possible in ADO.NET too? Kepp in mind that this is an in-memory table for offline ADO.NET since data is not meant to be persisted in any way.
Thank you
If what you're asking is if you can do date arithmetic in the query string, then no; the filtering and query functionality available in the DataTable
and DataView
are both very simple. Depending on what you're doing you may be better off using a LINQ query instead.
精彩评论