我有以下查询
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子句中,我实际上需要Min的合并DateOnly和TimeSpan值 d.RequestedDate [DateOnly] + d.RequestedDateTimeFrom [TimeSpan]在DB中看起来像这样:
RequestedDate : 2013-06-01 RequestedDateTimeFrom : 13:50
这不编译:
let firstCollection = (from d in collections where d.AddressType == BookingAddressType.Collection select d.RequestedDate + d.RequestedDateTimeFrom)
编辑:与此同时我想到了一种不同的方法,这实际上解决了我的主要问题,即如果有更多的日期时间同时具有相同的日期和时间值,我将通过thrid Sequence列进行比较。所以归结为简单排序:
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;
答案 0 :(得分:0)
这可能不是最优雅的方式(并且未经测试,说实话),但我会尝试在TimeSpan上使用SqlFunctions.DateAdd的符号化SqlFunctions.DatePart
SqlFunctions.DateAdd("hh",
SqlFunctions.DatePart("hh", d.RequestedDateTimeFrom),
SqlFunctions.DateAdd("mi",
SqlFunctions.DatePart("mi", d.RequestedDateTimeFrom),
d.RequestedDate);
(您也可以使用EntityFunctions.AddHours
和EntityFunctions.AddMinutes
代替SqlFunctions.DateAdd
)