2017-03-31 59 views
0

我試圖計算列表中列中標記爲休息的人數。協助簡單計數列表

當前當我運行我的foreach循環(底部的一個)來計算列出的斷點數時,它會拋出錯誤消息,無法將類型char轉換爲字符串。我明白我的NewAgent類不包含所有字符串值,但我只是試圖引用一個字符串。我需要一種方法來計算中斷出現在我的列表中的次數。突破將出現newAgent.auxreason

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

NewAgent newAgents = new ScoreBoardClientTest.NewAgent(); 

foreach (var item in e.CmsData.Agents) 
{ 


    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); 
} 

int breakCount = 0; 
foreach(string s in newAgents.AuxReasons) 
{ 
    if (s != null && s.StartsWith("Break")) breakCount++; 
} 
+1

你永遠不會改變'newAgents'在你的循環,你只是一遍又一遍地將相同的對象。這段代碼需要一些幫助 – Jonesopolis

回答

1

試試這個:

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; 

    newAgentList.Add(newAgents); 
} 
+1

這最終解決了我的問題。我標記你的答案,因爲你先發布。感謝您的幫助 – mcavanaugh418

0

第一招

NewAgent newAgents = new ScoreBoardClientTest.NewAgent(); 

int breakCount = 0; 
foreach(var agent in newAgentList) 
{ 
    if (!string.IsNullOrEmpty(agent.AuxReasons) && agent.AuxReasons.StartsWith("Break")) 
     breakCount++; 
} 

你也應該在每次迭代中創建新對象進入第一個lo OP所以如果你把 第二的newAgents將是新的,第二的foreach是一個字符串,它可以讓你字符的列表

1

首先,你需要把NewAgent newAgents = new ScoreBoardClientTest.NewAgent();你的第一個foreach循環中,因爲現在你的工作參照同一個對象,如果你在一個地方更新這個對象的任何屬性,它將被更新爲整個列表。第二,你需要在第二個循環中使用newAgentList而不是newAgents(這就是爲什麼你會看到異常,因爲你要通過字符串內部的字符而不是通過列表的元素)。

這應該工作:

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

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; 

    newAgentList.Add(newAgents); 
} 

int breakCount = 0; 
foreach(string s in newAgentList.AuxReasons) 
{ 
    if (!string.IsNullOrWhiteSpace(s.AuxReasons) && s.AuxReasons.StartsWith("Break")) breakCount++; 
} 
+0

您的答案和我的有什麼區別? – mm8

+0

當時我發佈這個答案我沒有看到你的 - 我是新來的,所以它花了我一段時間來正確地格式化我的答案,所以很抱歉。我會在下次發佈之前更新頁面,以確保沒有人在我之前。我的回答包含了他爲什麼面臨任何問題的簡要說明 – artgdev

1

好吧,首先,我們正在做一些在循環不好。

您聲明newAgents並一遍又一遍地設置它,因此它始終具有與e.CmsData.Agents中最後一項相同的值。例如,如果你有一個清單,以及AgName S IN列表中的項目有:

Bob 
Michael 
James 

newAgents總是會循環完成時有「詹姆斯」的AgentName,因爲它宣佈出來的循環的範圍。通過移動環路範圍內的NewAgent佔位符的聲明解決這個問題,就像下面:

List<NewAgent> newAgentList = new List<NewAgent>(); 
foreach (var item in e.CmsData.Agents) 
{ 
    NewAgent newAgents = new ScoreBoardClientTest.NewAgent(); 
    // perform your data transforms 
    newAgentList.Add(newAgents); 
} 

這將使所以你實際添加對應於你想要的數據元素到列表操作,並且不需要該變量存在於循環之外。

您是否試圖計算列表中每個代理的原因數量,還是您想要計算所有代理中的所有「中斷」原因?我問的原因是再次,你的迭代過程完成後,你正在迭代器變量上執行最後一個foreach循環。

計算所有元素的休息,做到這一點,而不是你的第二個循環:

int count = newAgentList.Sum(agent => 
    agent.AuxReasons.Count(reasons => 
     !string.IsNullOrEmpty(reasons) && reasons.StartsWith("Break"))); 

如果你想指望當你操作它的迭代器,使用內lambda函數在你的第一個循環像這樣:

foreach (var item in e.CmsData.Agents) 
{ 
    // other logic from above 
    int count = newAgents.AuxReasons.Count(r => 
     !string.IsNullOrEmpty(r) && r.StartsWith("Break"); 
    // do something with count before loop ends 
} 

如果你這樣做後一個版本,你會循環的迭代完成或將丟失之前,需要做一些與計數。

如果這一切還不清楚,這裏是你的代碼的完全修改的版本:

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

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; 

    newAgentList.Add(newAgents); 
} 

int breakCount = newAgentList.Count(agent => 
    !string.IsNullOrEmpty(agent.AuxReasons) && agent.AuxReasons.StartsWith("Break")); 
+0

所以我先試了一下你的。 char不包含StartsWith的定義是我得到的錯誤。並且不能將字符轉換爲字符串。 – mcavanaugh418

+0

我看到......'AuxReasons'是一個'string',所以你正在迭代它的每個字符。我已經更新了我的答案以反映後代的這一點,但爲了將來的參考,請注意,這是造成您最初問題的原因。 –

+0

感謝您的知識 – mcavanaugh418