1

我想过滤一个 Datatable 并且它可以工作,但是如果我搜索一个 DateTime 我会得到一个错误。

这是我的代码。我做错了什么?

 DataTable tb = DataBaseManager.GetRadiusDataTable(radiusconnectionstring, "marksullivan"); 

DataRow[] filteredRows = tb.Select("AcctStartTime LIKE '%" + searchstring + "%' OR AcctStopTime LIKE '%" + searchstring + "%' OR FramedIPAddress LIKE '%" + searchstring + "%'");
tb = filteredRows.CopyToDataTable();
this.ListView.DataSource = tb;
this.ListView.DataBind();

AcctStartTime:datetime AcctStopTime :datetime FramedIPAddress : varchar

The error: The Operation 'Like' could not to System.DateTime and System.String execute.

我怎样才能做到这一点?

4

1 回答 1

10

像这样在 RowFilter 中尝试 DateComparision

string filter = $"DateFrom > '{daDateFrom}' AND DateTo <= '{daDateTo}'";
tb.Select(filter)

或来自DataRow 过滤器示例

日期值包含在尖字符 # # 内。日期格式与不变或英语文化的 DateTime.ToString() 方法的结果相同。

[C#]

dataView.RowFilter = "Date = #12/31/2008#"          // date value (time is 00:00:00)
dataView.RowFilter = "Date = #2008-12-31#"          // also this format is supported
dataView.RowFilter = "Date = #12/31/2008 16:44:58#" // date and time value
于 2013-01-24T10:10:57.053 回答