Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 需要弄清楚如何从列表中提取名称和时间跨度_C#_Wpf - Fatal编程技术网

C# 需要弄清楚如何从列表中提取名称和时间跨度

C# 需要弄清楚如何从列表中提取名称和时间跨度,c#,wpf,C#,Wpf,我有一个列表,可以拉取时间跨度,选择并显示列表中最大的时间跨度。我需要找到一种方法来显示与该时间跨度相匹配的名称。不确定从哪里开始。有人能帮我开始吗。下面是我的一些代码 代码:您可以看到我从workmodedirectiondescription=AVAIL-IN的项目创建了一个时间跨度列表。我需要AgName=AVAIL-IN=and,然后匹配具有max time Listoftimespans.max的代理,并显示具有max time的代理的名称。我希望这是有道理的。我遇到的最大问题是,我可

我有一个列表,可以拉取时间跨度,选择并显示列表中最大的时间跨度。我需要找到一种方法来显示与该时间跨度相匹配的名称。不确定从哪里开始。有人能帮我开始吗。下面是我的一些代码

代码:您可以看到我从workmodedirectiondescription=AVAIL-IN的项目创建了一个时间跨度列表。我需要AgName=AVAIL-IN=and,然后匹配具有max time Listoftimespans.max的代理,并显示具有max time的代理的名称。我希望这是有道理的。我遇到的最大问题是,我可以创建一个单独的agname列表,但不知道如何匹配代理的时间来显示时间最长的名称。我无法将agname添加到时间跨度列表中,因为agname的类型为string而不是timespan

  var listofTimeSpans = new List<TimeSpan>();

      List<NewAgent> newAgentList = new List<NewAgent>();                  

     foreach (var item in e.CmsData.Agents)
     {
         NewAgent newAgents = new 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;

         newAgentList.Add(newAgents);

         if (item.WorkModeDirectionDescription == "AVAIL-IN")
         {
             listofTimeSpans.Add(timeSpanSince);
         }
         var comparetimeandname = new tuple<string, TimeSpan>(item.Agname, listoftimespans.max());

         max = comparetimeandname.item2;
         maxname = comparetimeandname.item1.

     }
var listofTimeSpans=new List();
List newAgentList=新列表();
foreach(e.CmsData.Agents中的var项)
{
NewAgent newAgents=newnewagent();
newAgents.AgentName=item.AgName;
newAgents.AgentExtension=item.Extension;
newAgents.AgentDateTimeChange=ConvertedDateTimeUpdate;
newAgents.AuxReasons=item.AuxReasonDescription;
newAgents.LoginIdentifier=item.LoginId;
newAgents.AgentState=item.WorkModeDirectionDescription;
var timeSpanSince=DateTime.Now-item.DateTimeUpdated;
newAgents.AgentDateTimeStateChange=timeSpanSince;
newAgentList.Add(newAgents);
if(item.WorkModeDirectionDescription==“AVAIL-IN”)
{
时间跨度列表。添加(时间跨度);
}
var comparetimeandname=新元组(item.Agname,listoftimespans.max());
max=comparetimeansdname.item2;
maxname=comparetimeandname.item1。
}
更新:发生在上述代码之后

  var availInAgents = newAgentList.Where(ag => ag.AgentState == "AVAIL-IN").ToList();

  availInAgents.Sort((t1, t2)=>t1.AgentDateTimeStateChange.CompareTo(t2.AgentDateTimeStateChange));

                    var minTimeSpanAgent = availInAgents.FirstOrDefault();
                    var maxTimeSpanAgent = availInAgents.LastOrDefault();

                    var min3 = availInAgents.Take(3).ToList();
                    var max3 = availInAgents.Reverse<NewAgent>().Take(3).ToList();

NextInLine.Text = string.Join(Environment.NewLine, min3);
var availingents=newAgentList.Where(ag=>ag.AgentState==“AVAIL-IN”).ToList();
排序((t1,t2)=>t1.AgentDateTimeStateChange.CompareTo(t2.AgentDateTimeStateChange));
var minTimeSpanAgent=availiangents.FirstOrDefault();
var maxTimeSpanAgent=availiangents.LastOrDefault();
var min3=availingents.Take(3.ToList();
var max3=availingents.Reverse().Take(3.ToList();
Text=string.Join(Environment.NewLine,min3);
编辑:截图
我想林克可以帮你

例如,如果您需要具有最大时间跨度的代理:

var maxAgent = newAgentList.Max(agnet => agnet.AgentDateTimeStateChange);
Console.writeLine(maxAgent.AgentName);

比如说,正如在评论中所讨论的,您收集了一个表示名称及其时间跨度的元组列表。顺便说一句,我不会在循环中这样做;我会在循环之后这样做:

var availInItems = 
    newAgentList
        .Where(ag => ag.WorkModeDirectionDescription == "AVAIL-IN")
        .Select(ag => 
            new Tuple<String, TimeSpan>(ag.AgentName, ag.AgentDateTimeStateChange))
        .ToList();
最低三,最高三:

var min3 = availInItems.Take(3).ToList();
//  There's another overload of Reverse, without the type argument, which 
//  is a meber of List<T>; it reorders the list in place and returns void. 
//  You need this one.
var max3 = availInItems.Reverse<Tuple<String, TimeSpan>>().Take(3).ToList();

列出名称/时间跨度对。也许不是将时间跨度保存在
时间跨度列表中,而是将该列表作为带有
的“AVAIL-in”
的代理列表。在该列表中找到具有最小和最大时间跨度的对象,您就知道了名称。成对的列表可以包含两种数据类型吗?时间跨度和字符串?
元组
是一个选项。另一种方法是使用Name和TimeSpan属性编写自己的quickie类。如果您要长期维护代码,我建议您使用后一种方法。因此,我将列表更改为SortedList()。它允许我同时添加agname和timespan,我只需要访问与最高时间匹配的agname。我必须研究元组,因为我以前从未使用过它。因此,如果多个名称具有相同的时间跨度,您想扔掉重复的名称吗?这应该是agnet还是AgName?您的答案也没有考虑到状态中的可用性。但无法分别打印名称和时间。它只抓取两个项目的两个值?@firehotguitar88什么在抓取什么?分别地我不明白。给我看看代码,这真的很有效。我只是有一个问题,直到我看到它的工作理解。很抱歉。你知道有没有办法同时得到第二和第三个人吗?你那惊人的Ed.@firehotguitar88这就是为什么。。。我是说,谢谢你。
var min3 = availInItems.Take(3).ToList();
//  There's another overload of Reverse, without the type argument, which 
//  is a meber of List<T>; it reorders the list in place and returns void. 
//  You need this one.
var max3 = availInItems.Reverse<Tuple<String, TimeSpan>>().Take(3).ToList();
var availInAgents = 
    newAgentList
        .Where(ag => ag.WorkModeDirectionDescription == "AVAIL-IN")
        .ToList();

availInAgents.Sort((t1, t2) => 
    t1.AgentDateTimeStateChange.CompareTo(t2.AgentDateTimeStateChange));

var minTimeSpanAgent = availInAgents.FirstOrDefault();
var maxTimeSpanAgent = availInAgents.LastOrDefault();

var min3 = availInAgents.Take(3).ToList();
var max3 = availInAgents.Skip(availInAgents.Count - 3).ToList();

max3.Reverse();