【问题标题】:How to filter datatable如何过滤数据表
【发布时间】:2016-03-28 15:59:10
【问题描述】:

我已将此数据表保存在缓存中

 DataTable dataTable = ReportsBLL.GetProducts() as DataTable;
 HttpContext.Current.Cache["productinfokey"] = dataTable;
 System.Web.HttpContext.Current.Cache.Insert("productinfokey", dataTable, null, DateTime.Now.AddHours(1), System.Web.Caching.Cache.NoSlidingExpiration);

有时我需要将数据表过滤为

dataTable.Select("CreateDate >= " + DateTime.Now.AddMonths(-3) + " and CreateDate <= " + DateTime.Now + "")

但是如何按这些属性过滤数据,因为这种方式会给我错误 Syntax error: Missing operand after '05' operator.

填充的数据表
 DataTable table = new DataTable();
 try
 {
      SqlCommand cmd = (SqlCommand).Database.Connection.CreateCommand();

      cmd.CommandText = "[dbo].[Report_Get]";
      cmd.CommandType = CommandType.StoredProcedure;
      cmd.Parameters.AddWithValue("@parmCategory", parmCategory);

      try
      {
           Connection.Open();
           var reader = cmd.ExecuteReader();
           table.Load(reader);
           return table;
      }
      catch { /*some other code*/ }
 }
 catch { /*some other code*/ }

这里出了什么问题,我该如何解决?

【问题讨论】:

  • 语法描述为here

标签: asp.net asp.net-mvc linq datatable datatables


【解决方案1】:

这个:

"CreateDate >= " + DateTime.Now.AddMonths(-3)

会产生这样的东西(当然,取决于文化设置,但这并不重要):

CreateDate >= 12/28/2015

这是一个语法错误。日期值需要用引号括起来:

CreateDate >= '12/28/2015'

所以你需要在字符串中考虑到这一点:

"CreateDate >= '" + DateTime.Now.AddMonths(-3) + "'"

【讨论】:

    猜你喜欢
    • 2018-04-17
    • 2021-09-02
    • 2017-11-14
    • 1970-01-01
    相关资源
    最近更新 更多