使用 DbFunctions return 包含分钟的日期时间
Use DbFunctions to return the dateTime with mins included
我有以下代码,它截断了时间:
// does this already exist?
var seekScan = (from s in aDb.Item_Scan_Event
where s.item_detail_id == th_piece_id &&
s.scan_type == scantype &&
DbFunctions.TruncateTime(s.scan_datetime) == dt_scan.s.ScanDatetime.Date
select s).FirstOrDefault();
我需要的是让它比较日期时间,包括 HH:mm
我试过使用一些简单的东西,比如字符串比较,但你不能在 Linq-to-Entities 中这样做:
var seekScan = (from s in aDb.Item_Scan_Event
where s.item_detail_id == th_piece_id &&
s.scan_type == scantype &&
s.scan_datetime.Value.ToString("MM/dd/yyyy HH:mm") == dt_scan.s.ScanDatetime.ToString("MM/dd/yyyy HH:mm")
select s).FirstOrDefault();
我得到的错误是:
LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression.'
看看这个FUNCTION database.AddDays does not exist
在 Mysql 我要创建函数,也许你需要在你的 bd 中查看函数。
不优雅,但有效。
//does this already exist? Ignore items scanned within the same minute.
var seekScan = (from s in aDb.Item_Scan_Event
where
s.item_detail_id == th_piece_id &&
s.scan_type == scantype &&
(s.scan_datetime.Value.Year == dt_scan.s.ScanDatetime.Year &&
s.scan_datetime.Value.Month == dt_scan.s.ScanDatetime.Month &&
s.scan_datetime.Value.Day == dt_scan.s.ScanDatetime.Day &&
s.scan_datetime.Value.Hour == dt_scan.s.ScanDatetime.Hour &&
s.scan_datetime.Value.Minute == dt_scan.s.ScanDatetime.Minute )
select s
).FirstOrDefault();
简化 并使用 DbFunctions
class :
var seekScan = (from s in aDb.Item_Scan_Event
where
s.item_detail_id == th_piece_id &&
s.scan_type == scantype && (
DbFunctions.TruncateTime(s.scan_datetime.Value).Value == dt_scan.s.ScanDatetime.Date &&
DbFunctions.DiffMinutes(s.scan_datetime.Value, dt_scan.s.ScanDatetime) == 0 )
select s
).FirstOrDefault();
更新
- We could just use the
DiffMinutes
and that would work unless it was that exact same time and a different day ?
时间跨度相同但日期不同的情况会失败
- does
DiffMinutes
ignore the date portion ? So 11/11/2019 11:02 vs 11/12/2019 11:03 would be 1 ?
我不确定 returns 日期和时间跨度之间的差异还是只是时间跨度,我已经很久没有使用它了。我猜你会测试它并告诉我们。
当您定义类型为 Datetime 的 属性 时,如果默认情况下没有为 属性 定义属性,它将保存时间,包括小时、分钟和秒。
所以根据以下情况是一些用例:
场景 1:仅检查 day/month/year
a) 对于日期时间
var loRecords = context.ORDERS.Where(x=>x.O_ORDER_TIME.Date.Equals(DateTime.Now.Date).ToList()
b) 对于日期时间?
var loRecords = context.ORDERS.Where(x=>x.O_ORDER_TIME?.Date.Equals(DateTime.Now.Date).ToList()
场景 2:检查 day/month/year 小时和分钟
a) 日期时间
var loRecords = context.ORDERS.Where(x=>x.O_ORDER_TIME.Date.Equals(DateTime.Now.Date)
&& x.O_ORDER_TIME.Date.Hour == DateTime.Now.Date.Hour
&& x.O_ORDER_TIME.Date.Minute == DateTime.Now.Date.Minute).ToList()
我有以下代码,它截断了时间:
// does this already exist?
var seekScan = (from s in aDb.Item_Scan_Event
where s.item_detail_id == th_piece_id &&
s.scan_type == scantype &&
DbFunctions.TruncateTime(s.scan_datetime) == dt_scan.s.ScanDatetime.Date
select s).FirstOrDefault();
我需要的是让它比较日期时间,包括 HH:mm
我试过使用一些简单的东西,比如字符串比较,但你不能在 Linq-to-Entities 中这样做:
var seekScan = (from s in aDb.Item_Scan_Event
where s.item_detail_id == th_piece_id &&
s.scan_type == scantype &&
s.scan_datetime.Value.ToString("MM/dd/yyyy HH:mm") == dt_scan.s.ScanDatetime.ToString("MM/dd/yyyy HH:mm")
select s).FirstOrDefault();
我得到的错误是:
LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression.'
看看这个FUNCTION database.AddDays does not exist
在 Mysql 我要创建函数,也许你需要在你的 bd 中查看函数。
不优雅,但有效。
//does this already exist? Ignore items scanned within the same minute.
var seekScan = (from s in aDb.Item_Scan_Event
where
s.item_detail_id == th_piece_id &&
s.scan_type == scantype &&
(s.scan_datetime.Value.Year == dt_scan.s.ScanDatetime.Year &&
s.scan_datetime.Value.Month == dt_scan.s.ScanDatetime.Month &&
s.scan_datetime.Value.Day == dt_scan.s.ScanDatetime.Day &&
s.scan_datetime.Value.Hour == dt_scan.s.ScanDatetime.Hour &&
s.scan_datetime.Value.Minute == dt_scan.s.ScanDatetime.Minute )
select s
).FirstOrDefault();
简化 DbFunctions
class :
var seekScan = (from s in aDb.Item_Scan_Event
where
s.item_detail_id == th_piece_id &&
s.scan_type == scantype && (
DbFunctions.TruncateTime(s.scan_datetime.Value).Value == dt_scan.s.ScanDatetime.Date &&
DbFunctions.DiffMinutes(s.scan_datetime.Value, dt_scan.s.ScanDatetime) == 0 )
select s
).FirstOrDefault();
更新
- We could just use the
DiffMinutes
and that would work unless it was that exact same time and a different day ?
时间跨度相同但日期不同的情况会失败
- does
DiffMinutes
ignore the date portion ? So 11/11/2019 11:02 vs 11/12/2019 11:03 would be 1 ?
我不确定 returns 日期和时间跨度之间的差异还是只是时间跨度,我已经很久没有使用它了。我猜你会测试它并告诉我们。
当您定义类型为 Datetime 的 属性 时,如果默认情况下没有为 属性 定义属性,它将保存时间,包括小时、分钟和秒。
所以根据以下情况是一些用例:
场景 1:仅检查 day/month/year
a) 对于日期时间
var loRecords = context.ORDERS.Where(x=>x.O_ORDER_TIME.Date.Equals(DateTime.Now.Date).ToList()
b) 对于日期时间?
var loRecords = context.ORDERS.Where(x=>x.O_ORDER_TIME?.Date.Equals(DateTime.Now.Date).ToList()
场景 2:检查 day/month/year 小时和分钟
a) 日期时间
var loRecords = context.ORDERS.Where(x=>x.O_ORDER_TIME.Date.Equals(DateTime.Now.Date)
&& x.O_ORDER_TIME.Date.Hour == DateTime.Now.Date.Hour
&& x.O_ORDER_TIME.Date.Minute == DateTime.Now.Date.Minute).ToList()