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

如何将其他字段添加到按l​​inq分组

如何解决如何将其他字段添加到按l​​inq分组

我必须选择分组依据,因为我需要获取某些项目的平均值和总和,我的问题是首先分组依据是基于month(m_date.Month),因此在分组依据中我没有可以再访问年份了,我的第二个问题是,如果我想在查询中拥有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; }
            
        }

解决方法

这正是group by的工作方式。如果您还需要访问Year,则需要将MonthYear分组在一起:

 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),}
             ).OrderBy(s=>s.Date).ToListAsync(); 

请参阅:Using group by on multiple columns

另一方面,当您按项目分组时,可以访问其他属性列表,例如g.ToList()。参见:LINQ Group By and select collection

,

每个StatMonth都有一个属性m_Date。您的GroupBy可以使StatMonths组的m_Date.Month值相等。

因此,2019年2月具有m_Date的StatMonth将与2020年2月和2018年2月具有所有m_Date的StatMonth属于同一组。

您写道:

...所以在组内,我不再有年份的权限,

这取决于您要对Year做些什么。

  • 是否要在同一年的同一个月使用m_Date将StatMonths组?那么您是否要使一组StatMonths 2019-02、2019-03、2019-04,...,2020-02、2020-03等?
  • 或者您想组成一个2月的子组:2019年的子组,2020年的子组,等等。同样,将3月的组和2019年的子组,2020年的子组相结合。

如果您查看GroupBy的通用方法语法,则会看到以下内容:

var result = db.StatMonths
    .Where(...)
    .GroupBy(
        // parameter KeySelector: I want to make Groups of StatMonths that have the 
        // same value for this key:
        statMonth => ... // this will be the key,// for example: groups with same Year/Month combination of m_Date
        statMonth => new 
        {
           Year = statMonth.m_Date.Year,Month = statMonth.m_Date.Month,},// parameter resultSelector,for every Key,and all StatMonths that have this key
        // make one new object:
        (yearMonthCombination,statMonthsWithThisYearMonthCombination) => new ...
}

因此,在参数resultSelector中,您将获得密钥以及具有此密钥的所有项目。因此,如果您需要具有相同的年/月组合的组,请如上所述创建密钥。如果您想将所有年份中的月份都划分为相同的月份,并且每年将其划分为子组:

// parameter keySelector: make groups with same month,for all year:
statMonth => stathMonth.m_Date.Month,// parameter resultSelector: take the key and all statMonths with this key to make a new
(month,statMonthsWithThisMonth) => new
{
     Month = month,SubGroups = statMonthsWithThisMonth.GroupBy(
         // make subgroups with same Year,// all statMonths in this group already have the same month
         statMonth => statMonth.m_Date.Year,// parameter resultSelector: use the year,and all statMonths of the group
         // with this year
         (year,statMonthisWithThisYear) => new
         {
              Year = year,... // other statMonth
         })
  })

除了您的示例外,您没有说您想做什么,所以我不知道将具有相同年/月组合的组序列或具有相同子组月份的序列进行分组是否更好同年。

恕我直言,后者使得在不增加太多功能的情况下很难看清结果。

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