数据库用的是SQL 2005 ,DateTime的格式如:2010/8/16 15:49:57
界面上用了一个日历控件来提供用户的输入,格式如:2010-8-16我要用Linq to sql查询语句,查出日期=2010-8-16的数据,应该怎么做?
如果数据库的日期字段是SetDate,日历控件的输入结果为time我的查询:from obj in query where obj.SetDate.Date.ToString("d") == time orderby obj.id descending select obj;在查询语句中直接转换日期格式,但是转换的日期格式为2010/8/16,而且报错:方法“System.String ToString(System.String)”不支持转换为 SQL。

解决方案 »

  1.   

    //这是一个例子:
                if (!string.IsNullOrEmpty(DateFrom))
                {
                    var tmpDateTime = new DateTime(Convert.ToInt32(DateFrom.Substring(6, 4)), Convert.ToInt32(DateFrom.Substring(3, 2)), Convert.ToInt32(DateFrom.Substring(0, 2)), 0, 0, 0);
                    
                    tempSrc = from tb in tempSrc where tmpDateTime.CompareTo(tb.DATE) <= 0 select tb;
                }
      

  2.   

    你这个DateFrom的格式是什么?
      

  3.   

    随便什么格式的字符串,
    用Substring()还不够灵活吗?
    要怎么弄还不是由你说了算!
      

  4.   

    Substring()是灵活,但是也要符合要求吧,你看我提供的字符串格式了吗?
    还有就是如果日期和月份是单数,前面不带0,你不是要硬给它塞给0进去啊?
      

  5.   

    在linq查询语句中使用ToString() 的实现方法 
      

  6.   


    int a = 1;
    a.toString("00");就能达到你的要求。