簡體   English   中英

c#查找LINQ查詢返回的列表中的項目,並將其值與列表中的另一項進行比較

[英]c# find item in list returned by LINQ query and compare its value with another item in list

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

錯誤報告錯誤

我不確定我是否完全理解這里的設計,在我看來,最好將事件存儲在Event表中,其中每一行都有一個StartEnd列。

但是,如您所述,您可以通過EventDateTime對列表中的相關項進行驗證,然后跟蹤lastEventType 這樣你可以檢查當前事件類型是否等於lastEventType然后它是一個錯誤。

例如:

public class Event
{
    public string EventType { get; set; }
    public DateTime? EventDateTime { get; set; }

    public override string ToString()
    {
        return $"{EventDateTime}: {EventType}";
    }
}

public class Program
{
    static void Main(string[] args)
    {            
        // Events for the day ordered by EventDateTime
        var events = new List<Event>
        {
            new Event {EventType = "JE",
                EventDateTime = DateTime.Parse("10/21/2018 12:23 PM")},
            new Event {EventType = "JE",
                EventDateTime = DateTime.Parse("10/21/2018 2:29 PM")},
            new Event {EventType = "JE",
                EventDateTime = DateTime.Parse("10/21/2018 2:40 PM")},
            new Event {EventType = "JS",
                EventDateTime = DateTime.Parse("10/21/2018 2:15 PM")},
            new Event {EventType = "JS",
                EventDateTime = DateTime.Parse("10/21/2018 3:12 AM")},
            new Event {EventType = "JS",
                EventDateTime = DateTime.Parse("10/21/2018 12:12 PM")},
        }.OrderBy(e => e.EventDateTime);

        // Initialize lastEventType to the opposite of the type of event we 
        // expect to see first (which is "JS" unless there are no "JS" events)
        var lastEventType = events.Any(e => e.EventType == "JS")
            ? "JE" : "JS";

        foreach (var e in events)
        {
            var errorMessage = string.Empty;

            // Check for error
            if (e.EventType == lastEventType)
            {
                Console.ForegroundColor = ConsoleColor.Red;

                // Create an error message
                var error = e.EventType == "JS"
                    ? "JS before JE"
                    : "JE before JS";

                var dateStr = e.EventDateTime?.ToShortDateString() ?? "[null date]";

                errorMessage = $" - On {dateStr} this error occurred: [{error}]";

                // Your code would include:
                // errorList.Add(errorMessage); 
                // errorListRow.Add(dc);
            }

            Console.WriteLine(e + errorMessage);
            Console.ResetColor();

            // Update the lastEventType with this event type
            lastEventType = e.EventType;
        }

        GetKeyFromUser("\nDone! Press any key to exit...");
    }
}

產量

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM