需要帮助拉动时间跨度中最长和最短的时间

Need Help pulling longest and shortest time in a timespan

我有一个列表,其中打印了我当前在我的应用程序中为每个登录代理显示的时间跨度变量。当列表打印在我的数据网格上时,每个代理都有一列,每个代理都有一个时间跨度。我正在尝试拉出哪个代理当前具有最长的时间跨度并将其显示在另一个变量中,以便我可以显示最长的可用时间而无需代理在 phone 上。我目前正在为我的列表使用 foreach 循环,并试图从我的时间跨度变量中提取时间,坦率地说,我想我不理解这个列表是如何工作的(这里是业余程序员)。下面列出的我只展示了我用于时间跨度数学的内容,然后是它下面的整个 foreach 循环。

var timeSpanSince = DateTime.Now - item.DateTimeUpdated;

newAgents.AgentDateTimeStateChange = timeSpanSince;

var listofTimeSpans = new List<TimeSpan>(AgentDateTimeStateChange);

var min = listofTimeSpans.Min();
var max = listofTimeSpans.Max();

我知道其中很多内容是不需要的,但我发布它是为了尝试了解我正在尝试完成的事情

foreach (var item in e.CmsData.Agents)
{
    NewAgent newAgents = new ScoreBoardClientTest.NewAgent();
    newAgents.AgentName = item.AgName;
    newAgents.AgentExtension = item.Extension;
    newAgents.AgentDateTimeChange = ConvertedDateTimeUpdated;
    newAgents.AuxReasons = item.AuxReasonDescription;
    newAgents.LoginIdentifier = item.LoginId;
    newAgents.AgentState = item.WorkModeDirectionDescription;

    var timeSpanSince = DateTime.Now - item.DateTimeUpdated;
    newAgents.AgentDateTimeStateChange = timeSpanSince;

    var listofTimeSpans = new List<TimeSpan>(AgentDateTimeStateChange);
    var min = listofTimeSpans.Min();
    var max = listofTimeSpans.Max();
    int breakCount = 0;
    foreach (var agent in newAgentList)
    {                             
       if (!string.IsNullOrEmpty(agent.AuxReasons) && agent.AuxReasons.StartsWith("Break"))
           breakCount++;
       }

       int lunchCount = 0;
       foreach (var agent in newAgentList)
       {                            
           if (!string.IsNullOrEmpty(agent.AuxReasons) && agent.AuxReasons.StartsWith("Lunch"))
               lunchCount++;
           }
           newAgentList.Add(newAgents);
           if (breakCount > 2)
           {
               Dispatcher.BeginInvoke(DispatcherPriority.Normal, Action)(() =>
               {
                   inBreakData.Foreground = new SolidColorBrush(Colors.Yellow);
                   inBreakData.Text = breakCount.ToString();
               }));
           }else if (breakCount > 3)
           {
               Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
               {
                  inBreakData.Foreground = new SolidColorBrush(Colors.Red);
                  inBreakData.Text = breakCount.ToString();
                }));
           }
           else
           {
               Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
               {
                   inBreakData.Foreground = new SolidColorBrush(Colors.Green);
                   inBreakData.Text = breakCount.ToString();
               }));
           }

           Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() => { inLunchData.Text = lunchCount.ToString(); }));
       }     

我觉得你想多了。你有一个对象 NewAgent。

将该对象中的 属性 定义为类似 TimeInState 的对象并使其只读:

public TimeSpan TimeInState {get { return DateTime.Now - AgentDateTimeChange; } }

当您基于 NewAgent 对象读取时,这将始终为您提供当前状态。您填写 AgentDateTimeChange(我假设这是它们所处状态的最后更新时间),然后 TimeInState 会为您返回。如果您有一个 NewAgents 列表,那么您始终可以按 TimeInState 进行排序以获取降序或升序列表:

(List<NewAgent>).OrderByDescending(o => o.TimeInstate);

根据我从评论中得到的信息,您遇到了 var listofTimeSpans = new List<TimeSpan>(AgentDateTimeStateChange);

的问题

您没有名为 AgentDateTimeStateChange 的变量,您有一个 NewAgent class 的 属性 具有该名称。所以这里有 2 个选项

var listofTimeSpans = new List<TimeSpan>();
listofTimeSpans.Add(newAgents.AgentDateTimeStateChange);

var listofTimeSpans = new List<TimeSpan>();
listofTimeSpans.Add(timeSpanSince);

它们中的任何一个都应该工作。让我知道这是否回答了您的问题。

更新

这将修复您的异常,但不会解决您的问题,因为您将在每次迭代中覆盖您的列表、最小值和最大值。 最简单的方法是将列表拉出循环并在其外部执行 min/max。

var listofTimeSpans = new List<TimeSpan>();
foreach(...) {
    ...
    listofTimeSpans.Add(timeSpanSince);
    ...
}
var min = listofTimeSpans.Min();
var max = listofTimeSpans.Max();

I'm trying to pull which agent currently has the longest timespan

如果你重新定相,那么你想要第一个代理按时间跨度从大到小排序

var agent = agents.OrderByDescending(a=>a.TimeSpan).FirstOrDefault();

其中 TimeSpan 是您计算的这个 timeSpan 已经持续了多长时间