0

我有以下查询

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 值 d.RequestedDate [DateOnly] + d.RequestedDateTimeFrom [TimeSpan] 的最小值,在 DB 中看起来像这样:

申请日期:2013-06-01
RequestedDateTimeFrom : 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;
4

1 回答 1

0

这可能不是最优雅的方式(老实说,未经测试),但我会尝试在 TimeSpan 上使用SqlFunctions.DateAddSqlFunctions.DatePart

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

(您也可以使用EntityFunctions.AddHoursandEntityFunctions.AddMinutes代替SqlFunctions.DateAdd

于 2013-07-04T14:12:55.667 回答