如何在Linq查询中获取分组结果的平均值 [英] How to get Average of grouped results in Linq query

查看:47
本文介绍了如何在Linq查询中获取分组结果的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个linq查询,它获取前12个月的评分:

I have a linq query that gets ratings from the previous 12 months:

var ratingsByMonth =
    from month in Enumerable.Range(0, 12)
    let key = new { Year = DateTime.Now.AddMonths(-month).Year, Month = DateTime.Now.AddMonths(-month).Month }
    join groupedRating in ratings on key
            equals new
            {
                groupedRating.RatingDate.Year,
                groupedRating.RatingDate.Month
            } into g
    select new {
        Date = key,
        Rating = g.Select(x=>x.Rating)
    };

ratingsByMonth从我的数据集中返回以下JSON:

ratingsByMonth returns the following JSON from my data set:

{Date: {Year: 2016, Month: 11}, Rating: [4]}
{Date: {Year: 2016, Month: 10}, Rating: [5, 5, 4]}
{Date: {Year: 2016, Month: 9}, Rating: []}
{Date: {Year: 2016, Month: 8}, Rating: []}
{Date: {Year: 2016, Month: 7}, Rating: []}
{Date: {Year: 2016, Month: 6}, Rating: []}
{Date: {Year: 2016, Month: 5}, Rating: []}
{Date: {Year: 2016, Month: 4}, Rating: []}
{Date: {Year: 2016, Month: 3}, Rating: []}
{Date: {Year: 2016, Month: 2}, Rating: []}
{Date: {Year: 2016, Month: 1}, Rating: []}
{Date: {Year: 2015, Month: 12}, Rating: []}

但是,我试图将评分"字段汇总为平均值".我已经尝试过了:

However I'm trying to aggregate the Rating field into an Average. I've tried this:

return Json(new
{
    Average = ratingsByMonth.Average(x=>x.Rating),
    Dates = ratingsByMonth.Select(x => Date)
});

但是我在平均值-"行上突出显示了以下错误:

But I get the following error highlighted on the Average - line:

无法隐式转换类型 'System.Collections.Generic.IEnumerable'到'long?'

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'long?'

我正在寻找的输出是:

{Date: {Year: 2016, Month: 11}, Rating: 4}
{Date: {Year: 2016, Month: 10}, Rating: 3.5}
{Date: {Year: 2016, Month: 9}, Rating: 0}
{Date: {Year: 2016, Month: 8}, Rating: 0}
{Date: {Year: 2016, Month: 7}, Rating: 0}
{Date: {Year: 2016, Month: 6}, Rating: 0}
{Date: {Year: 2016, Month: 5}, Rating: 0}
{Date: {Year: 2016, Month: 4}, Rating: 0}
{Date: {Year: 2016, Month: 3}, Rating: 0}
{Date: {Year: 2016, Month: 2}, Rating: 0}
{Date: {Year: 2016, Month: 1}, Rating: 0}
{Date: {Year: 2015, Month: 12}, Rating: 0}

尝试更改为以下内容:

return Json(new
{
    Average = ratingsByMonth.Select(x => x.Rating).SelectMany(x => x).Average(),
    Dates = ratingsByMonth.Select(x => x.Date.Month)
});

但是,这只为我提供了整个数据集的一个平均值"值,而不是每个日期的平均值.

However, this just gives me a single 'Average' value across the whole data set, rather than an average per date.

以下是示例数据:

RatingID    Rating  RatingDate
5           5   2016-10-09 20:11:29.590
11          0   2016-10-09 20:14:42.503
21          5   2016-10-09 20:24:15.667
60          4   2016-10-28 11:01:21.220
64          4   2016-11-03 16:30:47.657

因此,11月的平均值应该是4,10月的平均值应该是3.5(5 + 0 + 5 + 4/4)

So for November the average should be 4, and October should be 3.5 (5+0+5+4 / 4)

推荐答案

在原始查询中,您可能想要这样做.

In the original query you probably want to do this.

//other code removed for brevity
select new {
    Date = key,
    Rating = g.Count() > 0 ? g.Average(x => x.Rating) : 0
};

然后返回结果

return Json(ratingsByMonth);

这篇关于如何在Linq查询中获取分组结果的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆