在“日期”和“时间”列中拆分的DateTime的聚合

本文关键字:DateTime 拆分 日期 时间 | 更新日期: 2023-09-27 18:20:56

我有以下查询

from booking in query
join ba in Context.BookingAddresses on booking.Id equals ba.BookingId into collections
let firstCollection = (from d in collections where d.AddressType == BookingAddressType.Collection select d.RequestedDate).Min()                            
where
EntityFunctions.TruncateTime(queryArgs.DateFrom.Value) <= EntityFunctions.TruncateTime(firstCollection) &&
EntityFunctions.TruncateTime(queryArgs.DateTo.Value) >= EntityFunctions.TruncateTime(firstCollection)
select booking;

在let子句中,我实际上需要合并的DateOnly和TimeSpan值的Mind.RequestedDate[DateOnly]+d.RequestedDateTimeFrom[TimeSpan]在DB:中看起来像这样

申请日期:2013-06-01请求日期时间起始时间:13:50

这不会编译:

let firstCollection = (from d in collections where d.AddressType == BookingAddressType.Collection select d.RequestedDate + d.RequestedDateTimeFrom)

编辑:与此同时,我想到了一种不同的方法,这实际上会解决我的主要问题,即,如果有更多的日期和时间值相同,我会用三次序列列对它们进行比较。因此,它可以归结为简单的排序:

from booking in query
join ba in Context.BookingAddresses on booking.Id equals ba.BookingId into collections
let firstCollection = collections.OrderBy(c => c.RequestedDate).ThenBy(c => c.RequestedFromTime).ThenBy(c => c.Sequence).FirstOrDefault()
//(from d in collections where d.AddressType == BookingAddressType.Collection select d.RequestedDate).Min()
where
EntityFunctions.TruncateTime(queryArgs.DateFrom.Value) <= EntityFunctions.TruncateTime(firstCollection.RequestedDate) &&
EntityFunctions.TruncateTime(queryArgs.DateTo.Value) >= EntityFunctions.TruncateTime(firstCollection.RequestedDate)
select booking;

在“日期”和“时间”列中拆分的DateTime的聚合

这可能不是最优雅的方式(老实说,这是未经测试的),但我会在TimeSpan 上尝试将SqlFunctions.DateAdd与SqlFunctions.Date Part叠加

SqlFunctions.DateAdd("hh", 
                      SqlFunctions.DatePart("hh", d.RequestedDateTimeFrom), 
                      SqlFunctions.DateAdd("mi", 
                                            SqlFunctions.DatePart("mi", d.RequestedDateTimeFrom), 
                                            d.RequestedDate);

(您也可以使用EntityFunctions.AddHoursEntityFunctions.AddMinutes而不是SqlFunctions.DateAdd