Linq get sum of data group by date

2019-01-23 19:10发布

问题:

my data look like:

 read_date |  T1  |  T2  |
15.02.2000 |   2  |   3  |
16.02.2000 |   4  |   5  |
15.03.2000 |   2  |   3  |
16.03.2000 |   5  |   4  |

I want to get mountly sum of T1 and T2, like this:

 read_date |  T1  |  T2  |
   02.2000 |   6  |   8  |
   03.2000 |   7  |   7  |   

I try to write something like this:

var result = from s in meter_readings.Take(10)
             group s by new { s.read_date} into g
             select new
             {
                 read_date = g.Key.read_date,
                 T1 = g.Sum(x => x.T1),
                 T2 = g.Sum(x => x.T2)
             };

but this does not give expected data.Is there any example to give data hourly sums, daily sums, etc.

Thanks

回答1:

You should only take the year and month into account when grouping:

var result = 
    from s in  meter_readings.Take(10)
    group s by new { date = new DateTime(s.read_date.Year, s.read_date.Month, 1) } into g
    select new
    {
        read_date = g.Key.date,
        T1 = g.Sum(x => x.T1),
        T2 = g.Sum(x => x.T2)
    };


回答2:

First, I think you can skip the anonymous type:

var result = from s in meter_readings.Take(10) 
         group s by s.read_date into g 
         select new 
         { 
             read_date = g.Key, 
             T1 = g.Sum(x => x.T1), 
             T2 = g.Sum(x => x.T2) 
         };

Second, to group by month, use some value that will uniquely identify the month, like this:

var result = from s in meter_readings.Take(10) 
         group s by s.read_date.ToString("yyyy.MM") into g 
         select new 
         { 
             read_month = g.Key, 
             T1 = g.Sum(x => x.T1), 
             T2 = g.Sum(x => x.T2) 
         };


回答3:

Your current group by will group by the full date (ie 15.02.2000 instead of 02.2000). That will end up creating an individual group for every day, not month.

Add a .Month to the group by (assuming it's a date object):

group s by new { s.read_date.Month} into g


回答4:

    static void Main()
    {
        var list = new List<meter_reading>
                       {
                           new meter_reading {Date = new DateTime(2000, 2, 15), T1 = 2, T2 = 3},
                           new meter_reading {Date = new DateTime(2000, 2, 10), T1 = 4, T2 = 5},
                           new meter_reading {Date = new DateTime(2000, 3, 15), T1 = 2, T2 = 3},
                           new meter_reading {Date = new DateTime(2000, 3, 15), T1 = 5, T2 = 4}
                       };
            var sum = list
            .GroupBy(x => GetFirstDayInMonth(x.Date))
            .Select(item => new meter_reading
                                {
                                    Date = item.Key,
                                    T1 = item.Sum(x => x.T1),
                                    T2 = item.Sum(x => x.T2),
                                }).ToList();
    }

    private static DateTime GetFirstDayInMonth(DateTime dateTime)
    {
        return new DateTime(dateTime.Date.Year, dateTime.Date.Month, 1);
    }


回答5:

query.OrderBy(o => o.OrderDate)
                .GroupBy(o => DbFunctions.CreateDateTime(o.OrderDate.Year, o.OrderDate.Month, 1, 0, 0, 0))
                       .Select(group => new DateIncomeDto { Date = group.Key.Value, Income = group.Sum(item => item.PayFee ?? 0) });

It worked for me!