DataTable.Select question

  • Thread starter Thread starter Mika M
  • Start date Start date
M

Mika M

Hi!

An application adds DataRows into DataTable in random period of time,
and after adding new rows saves DataTable as XML-file. DataTable
contains DateTime Field named as "SaveTime", and some other fields...

About "SaveTime" field, schema looks like...

<xs:element name="SaveTime" type="xs:dateTime" minOccurs="0" />

Q: Is it possible to retrieve easily only those rows where rows are for
example at least 10 minutes old according "SaveTime"?

I mean something like...

Dim drArr As DataRow() = dt.Select("SaveTime >= 10min")

....so how to make filterExpression correctly if it is possible?
 
Hi Mika,

The easisest way would be to add a column to your DataTable, fill it
manually (foreach...) accordingly (extract minutes from SaveTime field) to
your needs.
And then filter on this new column.
 
The easisest way would be to add a column to your DataTable, fill it
manually (foreach...) accordingly (extract minutes from SaveTime field) to
your needs.
And then filter on this new column.

So add new column into datatable like...

Dim dc As DataColumn = New DataColumn
dc.ColumnName = "Mins"
dc.DataType = GetType(Integer)
dt.Columns.Add(dc)

....or maybe simply like ...

dt.Columns.Add(New DataColumn("Mins", GetType(Integer))) '// Not tested!

....but could it be possible to use dc.Expression = ??? somehow to
calculate mins automatically?
 
Hi Mika,

Mika M said:
So add new column into datatable like...

Dim dc As DataColumn = New DataColumn
dc.ColumnName = "Mins"
dc.DataType = GetType(Integer)
dt.Columns.Add(dc)

...or maybe simply like ...

dt.Columns.Add(New DataColumn("Mins", GetType(Integer))) '// Not tested!

...but could it be possible to use dc.Expression = ??? somehow to
calculate mins automatically?

Look at DataColumn.Expression help topic on what it is possible to do.
Theoretically it could be possible but certainly it is not easy...
 
Hi,

I think it could be like

Dim drArr As DataRow() = dt.Select("SaveTime >= #" &
dateadd(DateInterval.Minute,-10, dt.Compute("MAX(SaveTime)","")) & "#")
 
Back
Top