如何过滤数据表

时间:2016-03-28 15:59:11

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

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

 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*/ }

这里有什么问题,如何解决?

1 个答案:

答案 0 :(得分:1)

此:

numbers.xml

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

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

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

CreateDate >= 12/28/2015

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

CreateDate >= '12/28/2015'