2013-07-04 125 views
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和時間跨度值的最小值 d.RequestedDate [DateOnly] + d .RequestedDateTimeFrom [時間跨度]看起來像這樣的DB:

 
RequestedDate : 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; 

回答

0

這也許不是最優雅的方式(和未經考驗的,是誠實的),但我會嘗試的時間跨度的鱗片SqlFunctions.DateAddSqlFunctions.DatePart

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

(你也可以使用EntityFunctions.AddHoursEntityFunctions.AddMinutes代替SqlFunctions.DateAdd

+0

編輯:'mi'幾分鐘,不'mm'(這是個) –

+0

呀,似乎是一個不得已的解決方案,如果沒有WOR KS :)但我只是編輯了我的原始帖子,我是如何用不同的方法解決它的。這很簡單,不知道它以前怎麼沒有發生...... 但是如果有更好的解決方案來「合併」這些值,我仍然會感興趣。 – akrobet