我每15分钟记录一次数据到PowerStringHistorys和PowerCombinerHistorys表中。我是LINQ的新手,想弄清楚如何创建一个查询,按每个小时对数据进行分组,并在那一小时平均分配当前数据。以下是我迄今为止按小时分组使用LINQ
TimeZoneInfo easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTime UTC_StartingDate = TimeZoneInfo.ConvertTimeToUtc(StartingDate, easternZone);
DateTime UTC_EndingDate = TimeZoneInfo.ConvertTimeToUtc(EndingDate, easternZone);
var FirstQ = from p in db.PowerStringHistorys
join t in db.PowerStrings on p.string_id equals t.id
join u in db.PowerCombiners on t.combiner_id equals u.id
join s in db.PowerCombinerHistorys on p.recordTime equals s.recordTime
where p.recordTime >= UTC_StartingDate
where p.recordTime <= UTC_EndingDate
select new
{
Combiner = u.id,
Current = p.current,
RecordTime = p.recordTime,
Voltage = s.voltage
};
现在我需要GROUP BY组合和时间,所以我可以平均电流,得到每个组合的千瓦时指定的日期范围内的每一个小时。
我需要申请查询心中有数,这种简单的公式: (每小时平均瓦)/ 1000 =千瓦时
所以我将结束有点像下面。任何帮助将不胜感激。
Combiner 1 03/19/2012 1:0:0 1.85 Kwh
Combiner 1 03/19/2012 2:0:0 1.98 Kwh
Combiner 1 03/19/2012 3:0:0 2.05 Kwh
Combiner 1 03/19/2012 4:0:0 2.11 Kwh
Combiner 1 03/19/2012 5:0:0 2.01 Kwh
Combiner 1 03/19/2012 6:0:0 1.96 Kwh
Combiner 1 03/19/2012 7:0:0 1.85 Kwh
Combiner 2 03/19/2012 1:0:0 1.77 Kwh
Combiner 2 03/19/2012 2:0:0 1.96 Kwh
Combiner 2 03/19/2012 3:0:0 2.03 Kwh
Combiner 2 03/19/2012 4:0:0 2.11 Kwh
Combiner 2 03/19/2012 5:0:0 2.02 Kwh
Combiner 2 03/19/2012 6:0:0 1.98 Kwh
Combiner 2 03/19/2012 7:0:0 1.83 Kwh
Combiner 3 03/19/2012 1:0:0 1.77 Kwh
Combiner 3 03/19/2012 2:0:0 1.96 Kwh
Combiner 3 03/19/2012 3:0:0 2.03 Kwh
Combiner 3 03/19/2012 4:0:0 2.11 Kwh
Combiner 3 03/19/2012 5:0:0 2.02 Kwh
Combiner 3 03/19/2012 6:0:0 1.98 Kwh
Combiner 3 03/19/2012 7:0:0 1.83 Kwh
编辑
上面是我原来的问题。在处理了我收到的两条建议之后,我结束了下面显示的代码。现在我只是将日期和总Kwh返回到视图。我打算将stringGroupedKwhlist列表投影到HighChart中供用户查看并将第一个查询结果投射到Telerik网格中,供用户过滤/排序/分组,以便他们可以处理细节。虽然代码工作并产生了我期望的结果,但我不确定它是否有效。由于我必须循环使用foreach,我猜测一旦获得大量数据,它可能会减慢速度。有没有更有效的方式来处理这个问题?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using AESSmart.Models;
namespace AESSmart.Controllers
{
public class StringKwh
{
public int CombinerID;
public int StringID;
public DateTime Interval;
public Double KWH;
public StringKwh(int combiner, int stringid, DateTime interval, double kwh)
{
CombinerID = combiner;
StringID = stringid;
Interval = interval;
KWH = kwh;
}
}
public class HomeController : Controller
{
private readonly AESSmartEntities db = new AESSmartEntities();
public ActionResult Index()
{
//REPRESENTS DATE RANGE FOR A FULL DAY
DateTime startingDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 1);
DateTime endingDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);
TimeZoneInfo easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTime utcStartingDate = TimeZoneInfo.ConvertTimeToUtc(startingDate, easternZone);
DateTime utcEndingDate = TimeZoneInfo.ConvertTimeToUtc(endingDate, easternZone);
var firstQ = from p in db.PowerStringHistorys
from s in db.PowerCombinerHistorys
join t in db.PowerStrings on p.string_id equals t.id
join u in db.PowerCombiners on t.combiner_id equals u.id
where p.recordTime == s.recordTime
where p.recordTime >= utcStartingDate
where p.recordTime <= utcEndingDate
select new
{
Combiner = u.id,
StringId = p.string_id,
Current = p.current,
RecordTime = p.recordTime,
Voltage = s.voltage
};
var groups = firstQ.ToList().GroupBy(q => new
{
q.Combiner,
q.StringId,
Date = q.RecordTime.Date,
Hour = q.RecordTime.Hour
});
List<StringKwh> stringGroupedKwhlist = new List<StringKwh>();
foreach (var group in groups)
{
stringGroupedKwhlist.Add(new StringKwh(
group.Key.Combiner,
group.Key.StringId,
new DateTime(group.Key.Date.Year, group.Key.Date.Month, group.Key.Date.Day, group.Key.Hour, 0, 0),
group.Average(g => g.Voltage * g.Current)/1000d
));
}
var groupCombiner = stringGroupedKwhlist.GroupBy(q => new { q.CombinerID });
double myTotalKwh = 0;
foreach (var combinerGroup in groupCombiner)
{
myTotalKwh = Math.Round(combinerGroup.Sum(g => g.KWH), 3);
}
ViewBag.LifeTimeGeneration = myTotalKwh;
ViewBag.myUTCStartDate = utcStartingDate;
ViewBag.myUTCEndDate = utcEndingDate;
return View();
}
public ActionResult About()
{
return View();
}
}
}
感谢您的回复。其实我忘了提及我需要按字符串级别进行分组。所以我补充说。我能够让你的解决方案工作,但我确实有一个问题。请查看我上面的修改。 – Linger 2012-03-23 12:53:37