[linq+EF]用EF查詢資料的時候,linq語法不能用傳統function做DateTime比較

  • 686
  • 0
  • 2017-06-01

摘要:[linq+EF]用EF查詢資料的時候,不能用傳統function做DateTime比較


DateTime yesterday = DateTime.Now.AddDays(-1);
//因為有用到ToString("yyyyMMdd"),所以之後用foreach處理inputTxFormalsError的時候,就會出現錯誤無法進入回圈
var inputTxFormalsError = dbFormal.Input_tx.Where(it => it.apptime.ToString("yyyyMMdd") == DateTime.Now.AddDays(-1).ToString("yyyyMMdd") && it.stype == "B" && it.scat == "00" && it.qstatus == "31").Take(testCount);
//改成下面這樣才可以,必須改用EF專用的Function才可以
var inputTxFormals = dbFormal.Input_tx.Where(it => DbFunctions.CreateDateTime(it.apptime.Year,it.apptime.Month,it.apptime.Day,0,0,0) ==
	DbFunctions.CreateDateTime(yesterday.Year, yesterday.Month, yesterday.Day, 0, 0, 0) && it.stype == "B" && it.scat == "00" && it.qstatus == "31").Take(testCount);