LINQ平均时间跨度? [英] LINQ Average TimeSpan?
问题描述
我有一个包含了开始时间和事件的结束时间条目的数据库表。我想借这些条目,并查找事件的平均长度。减法是没有问题的。然而,当我试图让平均值,编译器会抱怨
I have a database table that contains entries for start time and end time of an event. I would like to take those entries and find the average length of an event. The subtraction is no problem. However, when I try to get the average value, the compiler complains.
下面是我当前的LINQ查询:
Here is my current LINQ query:
public TimeSpan? getAverageTime()
{
return (from EventTime in db.EventTimes
where EventTime.EventTimesID > 0
select (EventTime.TimeEnd - EventTime.TimeStart)).Average();
}
我也试过.SUM()和.Aggregate(),这样我可以有一些帮助,但没有被接受。
I also tried .Sum() and .Aggregate() so that I could have something to work with, but none are accepted.
什么是实现我目标的最佳方法是什么?
What is the best method for accomplishing my goal?
推荐答案
看来你必须使用LINQ2SQL语句SqlMethods.DateDiff(DATEDIFF函数如果SQL):
It seems that you have to use a SqlMethods.DateDiff in linq2sql statement (DATEDIFF function if SQL):
public int getAverageTime()
{
return (from EventTime in db.EventTimes
where EventTime.EventTimesID > 0
select (SqlMethods.DateDiffMinute(EventTime.TimeStart, EventTime.TimeEnd)).Average();
}
这篇关于LINQ平均时间跨度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!