【发布时间】:2020-09-15 06:33:37
【问题描述】:
我必须选择和分组,因为我需要获取某些项目的平均值和总和,我的问题首先是分组是基于月份(m_date.Month)所以在分组内我不可以访问 year 了,我的第二个问题是如果我想在我的查询中拥有来自 statisticsDaily 类的其他属性怎么办?使用此查询,我只能访问按字段分组,请看下面:
var rslt = await (from d in db.statMonth.Include(f=>f.MasterData).Where(d=>d.m_turbine_id == IPAddress.Parse(id) && d.m_date >= frm)
group d by d.m_date.Month into g
select new statisticsDaily
{
Date = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(g.Key),
Production = g.Sum(s => s.m_energy_prod),
m_wind_speed = g.Average(s => s.m_wind_speed),
Availability = g.Average(s => s.m_availability),
}
).OrderBy(s=>s.Date).ToListAsync();
我的 statsticDaily 课程是:
public class statisticsDaily
{
public string Date { get; set; }
public Nullable<float> Production { get; set; }
public Nullable<float> m_wind_speed { get; set; }
public Nullable<float> Availability { get; set; }
public string Comments { get; set; }
public string TurbineId { get; set; }
public string Countries { get; set; }
}
【问题讨论】:
标签: linq