linq group by和select in group by给出错误EFcore



我有一个类

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; }

}

我需要在一些字段上应用聚合函数,并从类中选择其余字段

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 new { d.m_date.Month, d.m_date.Year} into g 
select new statisticsDaily
{
Year = g.Key.Year// We can access Year now since we grouped by Year as well
Date = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(g.Key.Month),
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),
Comments=g.Select(s=>s.Comments).ToString(),
Countries=g.select(i=>i.Country).ToString()              
}
).OrderBy(s=>s.Date).ToListAsync();

它给了我错误:LINQ表达式"Select<v_stats_daily,字符串>(来源:GroupByShaperExpression:键选择器:CAST(DATE_PART('month',v.m_DATE((AS integer(,元素选择器:EntityShaperExpression:实体类型:v_stats_dailyValueBufferExpression:ProjectionBindingExpression:EmptyProjectionMemberIsNullable:False,选择器:(i(=>i.m_comment('无法翻译。以可翻译的形式重写查询,或者通过插入对AsEnumerable((、AsAsyncEnumerable(、ToList((或ToListAsync((的调用显式切换到客户端评估

这是我最接近修复您的查询的方法。

var rslt =
(
await
(
from d in db.statMonth.Include(f => f.MasterData)
where d.m_turbine_id == IPAddress.Parse(id)
where d.m_date >= frm
group d by new { d.m_date.Month, d.m_date.Year } into g
orderby g.Key.Month
select new
{
Year = g.Key.Year,
Date = g.Key.Month,
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),
Comments = g.Select(s => s.Comments).ToArray(),
Countries = g.Select(i => i.Country).ToArray(),
}
)
.ToListAsync()
)
.Select(g => new statisticsDaily
{
Year = g.Year
Date = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(g.Date),
Production = g.Production,
m_wind_speed = g.m_wind_speed,
Availability = g.Availability,
Comments = String.Join(", ", g.Comments),
Countries = String.Join(", ", g.Countries),
})
.ToList();

你能检查一下你现在犯了什么错误吗?

最新更新