how to get group by devicetimestamp and show sum.
linq query
List<Total_Power> Total_PowerList = db.Total_Power.Where(u => u.DeviceImei == RS.DeviceImei && (u.DeviceTimeStamp >= RS.FromDate && u.DeviceTimeStamp <= RS.ToDate)).OrderByDescending(u => u.DeviceTimeStamp).ToList();
Total_PowerList = from s in Total_PowerList
group s by new { date = new DateTime(s.DeviceTimeStamp.Date) } into g
select new
{
read_date = g.Key.date,
T1 = g.Sum(x => x.KWH),
T2 = g.Sum(x => x.KWH)
};
Exp op Date KWH KWHCount
datatype of KWH is float
sample data
DeviceTimeStamp KWH
2020-12-05 12:30:15.000 62869.04
2020-12-05 12:15:16.000 62840.59
2020-12-05 12:00:16.000 62811.74
2020-12-05 11:45:22.000 62778.39
Exp op
DeviceTimeStamp KWH KWHConsumption
2020-12-05 62869.04 57.3
By expected output second value isn't Sum but Max (end state of the day).
I don't know what is the third value in the expected result. I think it is difference between daily max KWH and min KWH but I'm not sure because value in expected output doesn't match the difference. The value 57.3 corresponds to the Max / Min difference after 12 o'clock (only PM).
Try use Linq similar to this:
var result = data
.GroupBy(
i => i.DeviceTimeStamp.Date,
(timeStamp, dayilyData) => new { timeStamp, dayilyData })
.Select(i => new
{
i.timeStamp,
finalState = i.dayilyData.Max(x => x.KWH),
consuption = (i.dayilyData.Max(x => x.KWH) - i.dayilyData.Min(x => x.KWH)) })
.ToList();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.