我有一天(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;
}