我的数据库表如下所示:
ScannerStatusHistoryID DetectorID TimeStamp HBD1 HBD2 HWD1 HWD2 RC1 RC2 RC3 RC4
1 948 2017-03-17 08:44:34.0000000 +01:00 3 3 3 0 3 3 0 3
2 948 2017-03-17 09:44:34.0000000 +01:00 3 3 3 0 3 3 0 3
3 948 2017-03-17 10:44:34.0000000 +01:00 3 2 3 0 3 3 0 3
4 948 2017-03-17 11:44:34.0000000 +01:00 3 2 3 0 3 3 0 1
5 948 2017-03-17 12:44:34.0000000 +01:00 3 2 3 0 3 3 0 1
我有这样的查询:
[Query]
public IQueryable<ScannerStatusHistory> GetScannerStatus(int detectorID)
{
var lastRow = ObjectContext.ScannerStatusHistories.Where(t => t.DetectorID == detectorID).OrderByDescending(d => d.TimeStamp).FirstOrDefault();
//Get last changed row
if (lastRow != null)
{
var lastChangeRow = ObjectContext.ScannerStatusHistories
.Where(t => t.DetectorID == detectorID
&& (t.HBD1 != lastRow.HBD1 || t.HBD2 != lastRow.HBD2 || t.HWD1 != lastRow.HWD1 || t.HWD2 != lastRow.HWD2 || t.RC1 != lastRow.RC1 || t.RC2 != lastRow.RC2 || t.RC3 != lastRow.RC3 || t.RC4 != lastRow.RC4))
.OrderByDescending(d => d.TimeStamp)
.FirstOrDefault();
//Return next row
if (lastChangeRow != null)
{
return ObjectContext.ScannerStatusHistories
.Where(x => lastChangeRow.TimeStamp < x.TimeStamp
&& x.DetectorID == detectorID)
.OrderBy(d => d.TimeStamp)
.Take(1);
}
}
return ObjectContext.ScannerStatusHistories.Where(t => t.DetectorID == detectorID).OrderBy(d => d.TimeStamp).Take(1);
}
它做什么,它搜索HBD1或HBD2或HWD1或HWd2或RC1或Rc2或Rc3或Rc4之间发生变化的最后一行。
根据示例我现在得到的结果是:
ScannerStatusHistoryID DetectorID TimeStamp HBD1 HBD2 HWD1 HWD2 RC1 RC2 RC3 RC4
4 948 2017-03-17 11:44:34.0000000 +01:00 3 2 3 0 3 3 0 1
但这只是因为1列已更改。所以它会抓住那一排。 但是,是否有可能为每行检查一次,并显示列已更改的日期。而不是1列已更改。
所以期望的结果是:
HBD1 = 2017-03-17 08:44:34.0000000 +01:00
HBD2 = 2017-03-17 10:44:34.0000000 +01:00
HWD1 = 2017-03-17 08:44:34.0000000 +01:00
HWD2 = 2017-03-17 08:44:34.0000000 +01:00
RC1 = 2017-03-17 08:44:34.0000000 +01:00
RC2 = 2017-03-17 08:44:34.0000000 +01:00
RC3 = 2017-03-17 08:44:34.0000000 +01:00
RC4 = 2017-03-17 11:44:34.0000000 +01:00
我现在收到的结果:
HBD1 = 2017-03-17 11:44:34.0000000 +01:00
HBD2 = 2017-03-17 11:44:34.0000000 +01:00
HWD1 = 2017-03-17 11:44:34.0000000 +01:00
HWD2 = 2017-03-17 11:44:34.0000000 +01:00
RC1 = 2017-03-17 11:44:34.0000000 +01:00
RC2 = 2017-03-17 11:44:34.0000000 +01:00
RC3 = 2017-03-17 11:44:34.0000000 +01:00
RC4 = 2017-03-17 11:44:34.0000000 +01:00
这可以在一个查询中执行,还是我必须执行8个单独的查询来检查每列的更改状态?