LINQ 比较同一年、不同月份的值,如果值发生变化则进行计数

LINQ compare values for the same year , different month and doing a count if value has changed

我有一个如下所示的数据集:

Option | Year | Month | Value 
-------+------+-------+------
   1   | 2011 |   12  |   0
-------+------+-------+------
   1   | 2011 |   11  |   1
-------+------+-------+------
   2   | 2012 |    6  |   0
-------+------+-------+------
   2   | 2012 |    7  |   0
-------+------+-------+------
   1   | 2011 |    6  |   2

我要找的结果集如下:

Option | Year | ChangedCount
-------+------+-------------
   1   | 2011 |      3
-------+------+-------------
   2   | 2012 |      0
-------+------+-------------

Changed Count 表示,如果该值在同一年的不同月份之间发生了变化。所以说如果 06 月的值是 2 然后 07 更改为 1 ,那么更改的计数将为 1 。如果两个月的值保持不变,则 changedCount 为 0

这是我到目前为止所写的

var changes = from ord in resultSet
    group ord by new
    {
        ord.Year,
        ord.Month,
        ord.Option,
        ord.Value,
    }
    into g
    select new
    {        
        Year = g.Key.Year,
        changed = g.Count(x => x.Value == 0) 
            + g.Count(x => x.Value == 1) 
            + g.Count(x => x.Value == 2)     
    };

如何运行 比较列中的先前值?

{0,1,2} 映射 ENUM 值

尝试:

var changes = from ord in resultSet
                       group ord by new
                       {
                          ord.Option,
                          ord.Year,
                       }
                       into g
                       select new
                       {
                          Option = g.Key.Option,
                          Year = g.Key.Year,
                          ChangedCount = g.Select(x => x.Value).Sum()
                       };

resultSet
  .GroupBy(x => new { x.Option, x.Year })
  .Select(x => new { x.Key.Option, x.Key.Year, ChangedCount = x.Select(x => x.Value).Sum() });

这是我从你的解释中了解到的:

class Record
        {
            public int Option { get; set; }
            public int Year { get; set; }
            public int Month { get; set; }
            public int Value { get; set; }
        }


var resultSet = new List<Record> {
                new Record { Option=1, Year=2011, Month=12, Value=0   },
                new Record { Option=1, Year=2011, Month=11, Value=1  },
                new Record { Option=2, Year=2012, Month=6, Value=0   },
                new Record { Option=2, Year=2012, Month=7, Value=0   },
                new Record { Option=1, Year=2011, Month=6, Value=2   },
            };

计算变化的辅助方法:

    public static int changeCount(List<Record> Records)
    {
        int previous = Records[0].Value;
        var result_change = 0;
        //i used sorted records by month you can do not this if order is not sensitive
        foreach (var rec in Records.OrderBy(x=>x.Month))
        {
            if (rec.Value != previous)
            {
                result_change++;
            }
            previous = rec.Value;
        }
        return result_change;
    }

和实际代码:

      var changes = resultSet.GroupBy(x => new { x.Year }).Select(g => new
        {
            Year = g.Key.Year,
            changed =changeCount( g.ToList()),
            Option = g.First().Option
        }).ToList();

结果:

 2011,3,1
 2012,0,2