gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 18:13:12 25 4
gpt4 key购买 nike

我有一个特定日期 (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 .

最佳答案

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

但是,如您所述,验证相关项目列表的一种方法是按 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...");
}
}

输出

enter image description here

关于c# 在 LINQ 查询返回的列表中查找项目并将其值与列表中的另一个项目进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53267928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com