遍历Linq到Entities结果,直到满足条件

时间:2018-11-09 14:37:35

标签: c# asp.net linq

在这种情况下,我有一个查询,该查询以dat返回事件。从班次开始(SS)开始,如果需要,我需要找到一个班次结束(SE),然后继续前进到下一个班次,否则,生成错误并记录其发生的行。

List<string> errorList = new List<string>();
List<int> errorListRow = new List<int>();
var itCompareDay = (from h in db.DailyGPSTables
                    where h.EmplID == EmpID
                       && (h.EventDateTime >= startDate
                       && h.EventDateTime <= endDate)
                       && (h.EventType == "SS" || h.EventType == "JS" || h.EventType == "LS" || h.EventType == "LE" || h.EventType == "JE" || h.EventType == "SE")
                    orderby h.EventDateTime
                    select h).ToList();
int rowNumber = -1;
foreach (DailyGPSTable e in itCompareDay)
{
   rowNumber = rowNumber + 1;
   string EOOmessage="";
   string eventText="";
   int dayCountSs = itCompareDay.Count(k => k.EventType == "SS" && (k.EventDateTime.Value.ToShortDateString() == e.EventDateTime.Value.ToShortDateString()));
   int dayCountSe = itCompareDay.Count(k => k.EventType == "SE" && (k.EventDateTime.Value.ToShortDateString() == e.EventDateTime.Value.ToShortDateString()));
   int dayCountJS = itCompareDay.Count(k => k.EventType == "JS" && (k.EventDateTime.Value.ToShortDateString() == e.EventDateTime.Value.ToShortDateString()));
   // Response.Write("<br> Count of event type is ss on " + e.EventDateTime.Value.ToShortDateString() + " is  " + itCompareDay.Count(k => k.EventType == "SS" && k.EventDateTime.Value.ToShortDateString() == e.EventDateTime.Value.ToShortDateString()));
   if (dayCountSs != dayCountSe)
   {
      eventText = "";
      if (dayCountSs > dayCountSe)
      {
         eventText = "Shift End (SE)";
      }
      else
      {
         eventText = "Shift Start (SS)";
      }
      EOOmessage = " On " + e.EventDateTime.Value.ToShortDateString() + " there is a missing " + eventText;
      errorList.Add(EOOmessage);
      errorListRow.Add(rowNumber);   
  }

上面的代码整天都返回一个错误,我正沿着这条路径走,以查找没有开始标记的行,并突出显示该行。一天可以有多个班次,但必须有结束标记。

if (dayCountSs != dayCountSe)
{
   eventText = "";
   if (dayCountSs > dayCountSe)
   {
      string dayEvents = e.EventType;
      Response.Write("SS greater than SE >Event Type " + dayEvents +"<BR>");
      for (int i = 0; i < dayCountSs; i++)
      {
         Response.Write("Should see a count here " + i);
         if (dayEvents[i].ToString() != "SS")
         {
            i = i + 1;
         }
         else
         {
            errorListRow.Add(rowNumber);
            eventText = "Shift End (SE)";
         }
      }
   }
   else
   {
      eventText = "Shift Start (SS)";
   }
   EOOmessage = " On " + e.EventDateTime.Value.ToShortDateString() + " there is a missing " + eventText;

1 个答案:

答案 0 :(得分:0)

使用other question中相同的扩展方法,相同的代码应该适用,仅适用于SS / SE,而不是JS / JE:

var shiftErrEvents = itCompareDay
                        .Select((ev, rowNum) => new { ev.EventType, ev.EventDateTime, rowNum })
                        .Where(cd => cd.EventType == "SS" || cd.EventType == "SE")
                        .GroupByWhile((pd, cd) => pd.EventType == "SS" && cd.EventType == "SE" && pd.EventDateTime.Date == cd.EventDateTime.Date)
                        .Where(cdg => cdg.Count() != 2)
                        .SelectMany(cdg => cdg.Select(cd => new { cd.rowNum, ErrMsg = cd.EventType == "SE" ? "SE without preceding SS" : "SS without following SE" }));

注意:在这两种情况下,跨越午夜边界(因此在不同日期)的工作或轮班将被视为两个不匹配的事件。