When using LINQ today, because the time in the database is saved in the varchar () format, the query time needs to be compared, so we use:
From J in system. jhzdwhere J. dwbm. tostring (). trim () = branchcode. tostring (). trim () & convert. todatetime (J. yearmonth. tostring (). trim ()). compareto (convert. todatetime (timefrom)> = 0 & convert. todatetime (J. yearmonth. tostring (). trim ()). compareto (convert. todatetime (timeto) <= 0 join B in system. t_sys_branchon J. dwbm. tostring (). trim () equals B. branchcodejoin C in system. t_cwlbon J. xmlb. tostring (). trim () equals C. idselect new {branchname = B. branchname, Category = C. CWMC, money = J. je, comefrom = J. zjly, time = J. yearmonth, zhaiyao = J. zhaiyao ,}
However, database reports are discovered during use. "Failed to convert from string to datetime".
I tried a lot of methods and still didn't use them. I finally found that the string of the yearmonth field in the database is"January 04, 2013",
When you change it to "", the query is normal and valid.