我有一张表格,大约每 15 分钟记录一次来自逆变器的信息。逆变器发送的数据之一是kwhtotal
表示通过该逆变器产生了多少总功率的数字。我正在尝试正确查询以获取特定日期的发电量。为此,我需要检索前一天的最后读数并将其与当天的最后读数进行比较。
这是我到目前为止所拥有的:
DateTime prevStartD = new DateTime((utcStartingDate.AddDays(i - 1)).Year, (utcStartingDate.AddDays(i - 1)).Month, (utcStartingDate.AddDays(i - 1)).Day, 0, 0, 0);
DateTime prevEndD = new DateTime((utcStartingDate.AddDays(i - 1)).Year, (utcStartingDate.AddDays(i - 1)).Month, (utcStartingDate.AddDays(i - 1)).Day, 23, 59, 59);
var previousDay = (from s in db.PowerInverterHistorys
join u in db.PowerInverters on s.inverter_id equals u.id
where u.name == record && (s.recordTime >= prevStartD && s.recordTime <= prevEndD)
orderby s.recordTime descending
select new
{
s.recordTime,
s.kwhtotal
}).Take(1);
DateTime currStartD = new DateTime((utcStartingDate.AddDays(i)).Year, (utcStartingDate.AddDays(i)).Month, (utcStartingDate.AddDays(i)).Day, 0, 0, 0);
DateTime currEndD = new DateTime((utcStartingDate.AddDays(i)).Year, (utcStartingDate.AddDays(i)).Month, (utcStartingDate.AddDays(i)).Day, 23, 59, 59);
var currentDay = (from s in db.PowerInverterHistorys
join u in db.PowerInverters on s.inverter_id equals u.id
where u.name == record && (s.recordTime >= currStartD && s.recordTime <= currEndD)
orderby s.recordTime descending
select new
{
s.recordTime,
s.kwhtotal
}).Take(1);
double? pDay = 0, cDay = 0;
foreach (var p in previousDay) { pDay = p.kwhtotal; }
foreach (var c in currentDay) { cDay = c.kwhtotal; }
var generatedPower = cDay - pDay;
它可以工作并运行,但是效率低下。页面在运行时打开需要很长时间。 我能做些什么来加快这个查询吗?我需要做的就是从kwhtotal
当天的最后一个条目中减去前一天kwhtotal
的最后一个条目。