本文介紹了Linq 按日期獲取數據組的總和的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我的數據如下:
read_date | T1 | T2 |
15.02.2000 | 2 | 3 |
16.02.2000 | 4 | 5 |
15.03.2000 | 2 | 3 |
16.03.2000 | 5 | 4 |
我想得到 T1 和 T2 的總和,像這樣:
I want to get mountly sum of T1 and T2, like this:
read_date | T1 | T2 |
02.2000 | 6 | 8 |
03.2000 | 7 | 7 |
我試著寫這樣的東西:
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.
謝謝
推薦答案
分組時只考慮年份和月份:
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)
};
這篇關于Linq 按日期獲取數據組的總和的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!