vb.net 序列不包含任何元素 - LINQ、MVC、Average

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13297880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:04:57  来源:igfitidea点击:

Sequence contains no elements - LINQ, MVC, Average

sqlasp.net-mvcvb.netlinqentity-framework

提问by Charlie

I am running into this error. I see that the reason is because the average returned at times is 0.00 which from a data stand point is accurate. This SQL query works fine, but that is because it puts in 0.00 automatically.

我遇到了这个错误。我看到原因是因为有时返回的平均值是 0.00,从数据的角度来看是准确的。这个 SQL 查询工作正常,但那是因为它自动输入 0.00。

LINQ complains and so I tried using DefaultIfEmpty() but it says it is expecting my ViewModel.

LINQ 抱怨,所以我尝试使用 DefaultIfEmpty() 但它说它期待我的 ViewModel。

Dim ticketCounts = From t In queue _
   Where _
    (t.StatusId = 2) And _
    (t.CloseDate.Year = Convert.ToDateTime(DateTime.Now).Year) And _
    (t.ResolutionDays > 0)
   Group t By _
    Column1 = CType(t.CloseDate.Month, Integer), _
    Column2 = CType(t.CloseDate.ToString("MMMM"), String) _
    Into g = Group _
   Order By Column1 _
   Select _
    Id = Column1, _
    Month = Column2, _
    Critical = g.Where(Function(t) t.PriorityId = 1).DefaultIfEmpty().Average(Function(t) t.ResolutionDays), _
    High = g.Where(Function(t) t.PriorityId = 2).DefaultIfEmpty().Average(Function(t) t.ResolutionDays), _
    Normal = g.Where(Function(t) t.PriorityId = 3).DefaultIfEmpty().Average(Function(t) t.ResolutionDays), _
    Low = g.Where(Function(t) t.PriorityId = 4).DefaultIfEmpty().Average(Function(t) t.ResolutionDays), _
    Total = g.Where(Function(t) t.Id <> Nothing).DefaultIfEmpty().Average(Function(t) t.ResolutionDays)

UPDATED!This is the SQL query doing the same thing I need VB to do.

更新!这是 SQL 查询,我需要 VB 做同样的事情。

SELECT
    DATENAME(MONTH,t.CloseDate) AS 'Month',
    AVG(CASE WHEN (t.PriorityId = 1) THEN CAST(t.ResolutionDays AS Decimal(18, 2)) ELSE 0 END) AS 'Critical',
    AVG(CASE WHEN (t.PriorityId = 2) THEN CAST(t.ResolutionDays AS Decimal(18, 2)) ELSE 0 END) AS 'High',
    AVG(CASE WHEN (t.PriorityId = 3) THEN CAST(t.ResolutionDays AS Decimal(18, 2)) ELSE 0 END) AS 'Normal',
    AVG(CASE WHEN (t.PriorityId = 4) THEN CAST(t.ResolutionDays AS Decimal(18, 2)) ELSE 0 END) AS 'Low',
    AVG(CAST(t.ResolutionDays AS Decimal(18, 2))) AS 'Monthly Average'
FROM
    tblMaintenanceTicket t
WHERE
    t.StatusId = 2
    AND YEAR(t.CloseDate) = year(getdate())
GROUP BY 
    MONTH(t.CloseDate),
    DATENAME(MONTH,t.CloseDate)
ORDER BY
    MONTH(t.CloseDate)

采纳答案by JaredPar

The problem is that all of the scalar LINQ methods (Average, Max, etc ...) throw an exception if the input IEnumerable(Of T)doesn't have any elements. It looks like the g.Wherecalls are resulting in an empty collection resulting in the exception.

问题是如果输入IEnumerable(Of T)没有任何元素,所有标量 LINQ 方法(Average、Max 等)都会抛出异常。看起来g.Where调用导致一个空集合导致异常。

What you may want to do is write a method which handles the empty case and returns a default value.

您可能想要做的是编写一个处理空情况并返回默认值的方法。

回答by Lachlan Ennis

回答by shtse8

This is the helper method I have used.

这是我使用的辅助方法。

    public static double AverageOrDefault(this IEnumerable<double> enumerable)
    {
        return enumerable.DefaultIfEmpty().Average();
    }

    public static double AverageOrDefault<T>(this IEnumerable<T> enumerable, Func<T, double> selector)
    {
        return enumerable.Select(selector).DefaultIfEmpty().Average();
    }