gpt4 book ai didi

c# - 使用 DbFunctions 返回包含分钟的日期时间

转载 作者:行者123 更新时间:2023-12-02 14:24:12 25 4
gpt4 key购买 nike

我有以下代码,它会截断时间:

// 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.'

最佳答案

不优雅,但有效。

//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();

关于c# - 使用 DbFunctions 返回包含分钟的日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58931678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com