如何使用GroupBy
查询TimeSpan
,LINQPad
查询LinqToSQL
选择SQL Server
平均值?
我已经尝试将所有内容转换为Ticks以及我能想到的所有其他内容但是我没有到达任何地方。
这是LINQ
查询,您可能只需要注意结束,但我发布所有内容以防万一:
from so in TblServiceOrders
where so.DateReceived!=null &&
so.TimeReceived != null &&
so.DateRequested!=null &&
so.TimeRequested != null &&
so.DateOpened != null &&
so.TimeOpened != null
let DayReceived = so.DateReceived.Value.DayOfWeek
let DayRequested = so.DateRequested.Value.DayOfWeek
let DayOpened = so.DateOpened.Value.DayOfWeek
////Work out Business Hours in SLA
let SLADaysLapesed = ((so.DateRequested.Value.Day) - (so.DateReceived.Value.Day))
let SLAContainsWE = (DayReceived == DayOfWeek.Monday && SLADaysLapesed <= 4 ||
DayReceived == DayOfWeek.Tuesday && SLADaysLapesed <= 3 ||
DayReceived == DayOfWeek.Wednesday && SLADaysLapesed <= 2 ||
DayReceived == DayOfWeek.Thursday && SLADaysLapesed <= 1 ||
DayReceived == DayOfWeek.Friday && SLADaysLapesed <= 0 ? false : true )
let SLATotalDailyAH = (new TimeSpan(0, (930 * SLADaysLapesed), 0))
let TotalWEHours = (SLAContainsWE == true ? new TimeSpan(0,1020,0) : new TimeSpan(0,0,0))
let SLAHours = (
((so.DateRequested.Value.AddHours(so.TimeRequested.Value.Hour)).AddMinutes(so.TimeRequested.Value.Minute)).Subtract
((so.DateReceived.Value.AddHours(so.TimeReceived.Value.Hour)).AddMinutes(so.TimeReceived.Value.Minute)).Subtract
(SLATotalDailyAH).Subtract(TotalWEHours)
)
////Work out Business Hours in Passed before Job Opened
let OpenDaysLapesed = ((so.DateOpened.Value.Day) - (so.DateReceived.Value.Day))
let OpenContainsWE = (DayReceived == DayOfWeek.Monday && OpenDaysLapesed <= 4 ||
DayReceived == DayOfWeek.Tuesday && OpenDaysLapesed <= 3 ||
DayReceived == DayOfWeek.Wednesday && OpenDaysLapesed <= 2 ||
DayReceived == DayOfWeek.Thursday && OpenDaysLapesed <= 1 ||
DayReceived == DayOfWeek.Friday && OpenDaysLapesed <= 0 ? false : true )
let OpenTotalDailyAH = (new TimeSpan(0, (930 * OpenDaysLapesed), 0))
let TotalOpenWEHours = (OpenContainsWE == true ? new TimeSpan(0,1020,0) : new TimeSpan(0,0,0))
let OpenHours = (
((so.DateOpened.Value.AddHours(so.TimeOpened.Value.Hour)).AddMinutes(so.TimeOpened.Value.Minute)).Subtract
((so.DateReceived.Value.AddHours(so.TimeReceived.Value.Hour)).AddMinutes(so.TimeReceived.Value.Minute)).Subtract
(OpenTotalDailyAH).Subtract(TotalOpenWEHours)
)
group new {SLAHours, OpenHours} by "Total" into g
select new {
SLAHour = g.Average (x => x.SLAHours.Ticks),
OpenHour = g.Average (x => x.OpenHours.Ticks)
}
这是我在尝试执行查询时得到的StackTrace
:
StackTrace at System.Data.Linq.SqlClient.PostBindDotNetConverter.Visitor.VisitMethodCall(SqlMethodCall mc)
at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)
at System.Data.Linq.SqlClient.SqlVisitor.VisitExpression(SqlExpression exp)
at System.Data.Linq.SqlClient.PostBindDotNetConverter.Visitor.VisitMethodCall(SqlMethodCall mc)
at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)
at System.Data.Linq.SqlClient.SqlVisitor.VisitExpression(SqlExpression exp)
at System.Data.Linq.SqlClient.PostBindDotNetConverter.Visitor.VisitMethodCall(SqlMethodCall mc)
at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)
at System.Data.Linq.SqlClient.SqlVisitor.VisitExpression(SqlExpression exp)
at System.Data.Linq.SqlClient.PostBindDotNetConverter.Visitor.VisitMember(SqlMember m)
at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)
at System.Data.Linq.SqlClient.SqlVisitor.VisitExpression(SqlExpression exp)
at System.Data.Linq.SqlClient.SqlVisitor.VisitUnaryOperator(SqlUnary uo)
at System.Data.Linq.SqlClient.PostBindDotNetConverter.Visitor.VisitUnaryOperator(SqlUnary uo)
at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)
at System.Data.Linq.SqlClient.SqlVisitor.VisitExpression(SqlExpression exp)
at System.Data.Linq.SqlClient.SqlVisitor.VisitRow(SqlRow row)
at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)
at System.Data.Linq.SqlClient.SqlVisitor.VisitSelectCore(SqlSelect select)
at System.Data.Linq.SqlClient.PostBindDotNetConverter.Visitor.VisitSelect(SqlSelect select)
at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)
at System.Data.Linq.SqlClient.SqlVisitor.VisitAlias(SqlAlias a)
at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)
at System.Data.Linq.SqlClient.SqlVisitor.VisitSource(SqlSource source)
at System.Data.Linq.SqlClient.SqlVisitor.VisitSelectCore(SqlSelect select)
at System.Data.Linq.SqlClient.PostBindDotNetConverter.Visitor.VisitSelect(SqlSelect select)
at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)
at System.Data.Linq.SqlClient.SqlVisitor.VisitAlias(SqlAlias a)
at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)
at System.Data.Linq.SqlClient.SqlVisitor.VisitSource(SqlSource source)
at System.Data.Linq.SqlClient.SqlVisitor.VisitSelectCore(SqlSelect select)
at System.Data.Linq.SqlClient.PostBindDotNetConverter.Visitor.VisitSelect(SqlSelect select)
at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)
at System.Data.Linq.SqlClient.PostBindDotNetConverter.Convert(SqlNode node, SqlFactory sql, ProviderMode providerMode)
at System.Data.Linq.SqlClient.SqlProvider.BuildQuery(ResultShape resultShape, Type resultType, SqlNode node, ReadOnlyCollection`1 parentParameters, SqlNodeAnnotations annotations)
at System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations)
at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
at System.Data.Linq.DataQuery`1.System.Collections.IEnumerable.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
HelpLink null
Source System.Data.Linq
HResult -2146233067
正如您可能知道的那样,我正在尝试使用LinqToSQL
LINQPad4
查询中运行此查询
如果我运行查询并选择全部“g”,则输出如下所示:
SLAHours | OpenHours
----------------------------
1.01:00:00 | 01:34:00
27.02:32:00 | 125.06:06:00
121.21:05:00 | 06:34:00
29.17:36:00 | 1.04:44:00
4.11:10:00 | 01:35:00
4.19:26:00 | 1.03:26:00
27.14:29:00 | 1.19:12:00
04:01:00 | 00:14:00
== EDIT == 下面添加了一个更小的例子,似乎有一个类似的问题,我试图克服这个问题:
from so in TblServiceOrders
where so.DateRequested!=null &&
so.TimeRequested != null
let SLAHours = (so.DateRequested.Value.AddHours(so.TimeRequested.Value.Hour)).AddMinutes(so.TimeRequested.Value.Minute)
group new {open = SLAHours} by "Total" into g
select g.Average (x => x.open.Ticks)
短错误:成员'System.DateTime.Ticks'没有支持的SQL转换。