下面是與日期時間在一個單一的領域之中(LINQpad查詢)對LINQ到SQL(上SQL Server)的解決方案:
Const BaseDate As Date = #9/24/2012#
Const PeriodMinutes As Integer = 15
Dim p = From t In TestData _
Group By q=CInt(Math.Floor((t.SomeDate - BaseDate).TotalMinutes/PeriodMinutes)) Into Group _
Select s=BaseDate.AddMinutes(q*PeriodMinutes), _
e=BaseDate.AddMinutes((q+1)*PeriodMinutes), _
ids=Aggregate g In Group Select g.ProcessID Distinct Into Count()
p.Dump
使用不同類型和其他「小」的調整可能產生更簡單的SQL後端代碼。
對於您的實際數據庫,您需要調整Group By
,儘管它現在取決於您的時間列通過Linq-to-SQL和/或數據庫的驅動程序轉換爲什麼。
由於它是一個TimeSpan
的Group By
變成類似:
Group By d=t.[Date], q=CInt(Math.Floor(t.[Time].TotalMinutes/PeriodMinutes)) Into Group _
Select s=d.AddMinutes(q*PeriodMinutes), _
e=d.AddMinutes((q+1)*PeriodMinutes), _
ids=Aggregate g In Group Select g.ProcessID Distinct Into Count()
是'類型的Date''Date'和'類型的時間...?爲什麼不使用一列而是兩列呢?你有什麼嘗試?如果可能的話,你應該在dbms中做。 –
是,日期列是一個日期類型和時間列是時間類型。我必須處理這個問題,因爲它是由我的erp軟件提供的。 – user1434532
什麼是「時間」類型?我只知道'TimeSpan' –