微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

linq分组依据并选择内部分组依据给出错误EFcore

如何解决linq分组依据并选择内部分组依据给出错误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 ( 来源:GroupByShaperExpression: 键选择器:CAST(DATE_PART('month',v.m_date))AS整数), ElementSelector:EntityShaperExpression: 实体类型:v_stats_daily ValueBufferExpression: ProjectionBindingExpression:空投影成员 IsNullable:错误 , 选择器:(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();

您可以检查一下现在遇到什么错误吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。