C#在LINQ查询返回的列表中查找项目,并将其值与列表中的另一个项目进行比较

时间:2018-11-12 18:18:55

标签: c# asp.net linq

我有一天(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行,并在没有行的情况下创建错误条件。

更新:

我已经能够创建一个包含事件类型和时间的列表。 List of events and times

我将如何遍历该列表,并确保第一个事件是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;
}

Errors incorrectly reported

1 个答案:

答案 0 :(得分:0)

我不确定我是否完全了解这里的设计,在我看来,最好将事件存储在Event表中,其中每行都有一个Start和{ {1}}列。

但是,如上所述,您可以验证相关项目列表的一种方法是按End排序列表,然后跟踪EventDateTime。这样,您可以检查当前事件类型是否等于lastEventType,这是错误的。

例如:

lastEventType

输出

enter image description here