【发布时间】:2019-04-15 12:20:41
【问题描述】:
我有一个在某一天 (d) 从 LINQ 查询返回的事件列表。
var findJSE = from a in db.DailyGPSTables
where (a.EventType == "JE" || a.EventType == "JS") &&
a.EventDateTime.Value.Day == d.Day select a;
我想比较每组匹配的 JS 和 JE 的时间,看看它们的顺序是否正确,例如。 JS(工作开始)在 JE(工作结束)之前。如果只有一个JS和一个JE,我可以通过使用这个来完成。
foreach (DailyGPSTable e in itCompareDay)
{
if (e.EventType == "JE" && e.EventDateTime.Value.Day == d.Day)
{
var findJS = from a in db.DailyGPSTables where a.EventType == "JS" && a.EventDateTime.Value.Day == d.Day select a;
var time = findJS.FirstOrDefault();
js = time.EventDateTime.Value;
if (js > e.EventDateTime.Value)
{
EOOmessage = " On " + e.EventDateTime.Value.ToShortDateString() + " JE before JS";
errorList.Add(EOOmessage);
errorListRow.Add(dc);
}
}
}
但是,如果一天中有多个 JS 是允许的,我会遇到一些麻烦。 @NetMage 为我提供了一个极好的潜在修复,但没有时间尝试正确实施它。我最近的尝试在这里:
//Response.Write("<br>3rd Condition number of JE and JS on " + e.EventDateTime.Value.Day + " match now we see if there is more than one js");
if (findJS.Count() > 1)
{
//Response.Write("<br>3.1 Condition there is more than one JS on " + e.EventDateTime.Value.Day + " get time of first js tag find the time of the first je tag");
var jsTime = findJS.ToList();
var jeTime = findJE.ToList();
for (int j = 0; j <= jsTime.Count - 1; j++)
{
//Response.Write("<br><br>The " + j + " " + jsTime[j].EventType + " Starts on " + jsTime[j].EventDateTime + "<br>");
var jsTimefor = jsTime[j].EventDateTime;
for (int k = 0; k <= jeTime.Count - 1; k++)
{
//Response.Write("<br><br>The " + k + " " + jeTime[k].EventType + " Starts on " + jeTime[k].EventDateTime + "<br>");
var jeTimefor = jeTime[k].EventDateTime;
if (jeTime[k].EventDateTime < jsTime[j].EventDateTime)
{
//Response.Write(" The " + k + " " + jeTime[k].EventType + " Starts on " + jeTime[k].EventDateTime + " and < " + jsTime[j].EventDateTime + " which is " + jsTime[j].EventType + "<br>");
EOOmessage = " On " + e.EventDateTime.Value.ToShortDateString() + " JE before JS";
errorList.Add(EOOmessage);
errorListRow.Add(dc);
}
}
}
}
此尝试突出显示当天的所有 JE 行,并在没有的情况下创建错误条件。
更新:
我将如何遍历该列表并确保第一个事件是 JS 并且它是最早的,下一个事件是 JE 并且按时间顺序是下一个。如果不报错。
我正在努力巩固和循环几天。这个21号只报一个错误,但是23号和27号都有错误。
for (DateTime d = startDate.Date; d <= endDate.Date; d = d.AddDays(1))
{
Response.Write("<br>" + d + "<br>");
var itCompareDay = (from h in db.DailyGPSTables
where h.EmplID == EmpID
&& (h.EventDateTime.Value.Day == d.Day)
&& (h.EventType == "SS" || h.EventType == "JS" || h.EventType == "LS" || h.EventType == "LE" || h.EventType == "JE" || h.EventType == "SE")
orderby h.EventDateTime
select h).ToList();
string EOOmessage="";
var lastEventType = itCompareDay.Any(x => x.EventType == "JS")? "JE" : "JS";
foreach (DailyGPSTable e in itCompareDay)
{
Response.Write("<br>Event Type " + e.EventType + " Count " + dc + " for the " + d.Day + "<br>");
if (e.EventDateTime.Value.Day == d.Day)
{
if (e.EventType == lastEventType)
{
var error = e.EventType == "JS"
? "JS before JE"
: "JE before JS";
Response.Write("<br>OUT OF SEQUENCE JE" + " ON " + e.EventDateTime.Value.ToShortDateString());
}
lastEventType = e.EventType;
}
dc = dc + 1;
}
rowNumber = rowNumber + dc;
}
【问题讨论】:
-
如果一天中有多个 JS 是允许的,我会遇到一些麻烦 - 如果你一天中有多个 JobStart,不能你只拿最新的和 JobEnd 比较一下?
-
@stuartd 不确定我是否关注您,您能否将您的问题放入代码中以便我详细说明?
-
也许考虑一下您将如何手动执行此操作,使用事件卡列表,其中一些标记为 JS/JE。只穿过甲板一次。如果没有 JS 事件,您的(一对)代码将失败。您无需继续重复基本查询中的
day测试。 -
@NetMage 请查看更新。