sql: select datediff(dd,convert(datetime,'2010/11/2'),GETDATE())
換成Linq:
var pcrDetail = from p in db.PcrDetail
                        where new string[] { "Open", "Reject" }.Contains(p.Task_Status) && !p.Project_No.ToLower().StartsWith("tmp")
                        && SqlMethods.DateDiffDay(SqlMethods.DateDiffDay( DateTime.Now) >= minDay && SqlMethods.DateDiffDay(DateTime.Parse(p.Task_DueDate), DateTime.Now) <= maxDay
                        select p;
        int j = 0;報錯:說Linq to sql不支持在裏面直接寫DateTime.Parse(p.Task_DueDate).請問這裡應該怎樣寫(不在外面再定義時間格式的參數)?

解决方案 »

  1.   

    自己已解決.
    Linq to sql不支持DateTime.Parse(p.Task_DueDate)這種寫法
    但支持System.Convert.ToDateTime(p.Task_DueDate)這種寫法
      

  2.   

    使用 Convert.ToDateTime()不要使用 DateTime.Parse()
      

  3.   

    不行啊!
    LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression. 
      

  4.   

    楼主是想问sql中如何将string类型转换为datetime类型吗?看下面的方法://最关键的语句:cast(TestTime as datetime)
    select cast(TestTime as datetime) from TestTable// 其中TestTime为String类型,查询的时候转换为datetime类型了
      

  5.   

    Linq to EF 不认 Convert.ToDateTime
    可在 db.表名 后加上.ToList()即可