我有一個列表,我得到列表中的每個項目在foreach
循環中。詞典檢查
private void saveButton_Click(object sender, EventArgs e)
{
if (saveFile1.ShowDialog() == DialogResult.OK)
{
StreamWriter sw = new StreamWriter(saveFile1.FileName);
int i = 1;
Dictionary<string, int> dictionary = new Dictionary<string, int>();
foreach (var line in theFinalList)
{
//compareList.Add(line.PartNumber[]);
if (!line.Name.Contains("FID") || !line.PartNumber.Contains("FIDUCIAL") ||
!line.PartNumber.Contains("EXCLUDES") || !line.PartNumber.Contains("excludes"))
{
//This doesn't work obviously.. :/
sw.WriteLine("({0} 0 1 1 0 0 0 0 \"{1}\" \"{2}\" \"\" {3} 0 0 0 0 0)",
i, line.PartDescription, line.PartNumber, partNumberOccurrences);
}
i++;
}
}
,我使用的字典存儲在字典中的線的各個部分,然後遞增,每當發現爲重複的itemCounter有麻煩。我試圖做這樣的事情,但我無法弄清楚它的邏輯。
我想只輸出部分編號1的時間以及它發生的次數以及與partNumber在同一行的相應值。
輸出應該是這樣的:
(1 0 1 1 0 0 0 0 "2125R_1.0" "136380" "" 18 0 0 0 0 0)
(2 0 1 1 0 0 0 0 "2125R_1.0" "128587" "" 41 0 0 0 0 0)
(3 0 1 1 0 0 0 0 "2125R_1.0" "138409" "" 11 0 0 0 0 0)
(4 0 1 1 0 0 0 0 "2125R_1.0" "110984" "" 8 0 0 0 0 0)
(5 0 1 1 0 0 0 0 "3216R_1.3" "114441" "" 6 0 0 0 0 0)
(6 0 1 1 0 0 0 0 "3216C_1.3" "2006722" "" 16 0 0 0 0 0)
(7 0 1 1 0 0 0 0 "3216C_1.3" "135732" "" 6 0 0 0 0 0)
誰能幫我創建一個字典來做到這一點?
你不需要'if';如果找不到密鑰,'TryGetValue'將把'default(int)'(aka 0)放入'value'中。 – dlev
不過,它的可讀性更強,如果... –
@Paolo嗯......好吧,公平的觀點:) – dlev