C# 向字典中累积添加值
我试图在字典中添加值,因为它们是根据特定条件确定的。应用程序必须循环每一行,一旦满足某个条件,就必须向字典中添加一个值。下面是一段代码,它是一项通过行进行循环并确定要添加的值的任务C# 向字典中累积添加值,c#,dictionary,C#,Dictionary,我试图在字典中添加值,因为它们是根据特定条件确定的。应用程序必须循环每一行,一旦满足某个条件,就必须向字典中添加一个值。下面是一段代码,它是一项通过行进行循环并确定要添加的值的任务 foreach (var line in this.FileLines) { count++; string[] bits = line.Split(','); fineNumber = bits[0].Trim(); int length
foreach (var line in this.FileLines)
{
count++;
string[] bits = line.Split(',');
fineNumber = bits[0].Trim();
int length = bits.Length;
if (length == 9)
{
//other processing gets done here, code not included as its of no interest for this question
}
else
{
//AddErrorFinesToFile(line, fineNumber);
AddFinesToDictonary(fineNumber, line);
continue;
}
}
下面是实际的方法签名及其代码,在这个方法中,我只是简单地尝试在字典中添加值
public Dictionary<string, string> AddFinesToDictonary(string fineNumber, string errorLine)
{
Dictionary<string, string> erroredLines = new Dictionary<string, string>();
erroredLines.Add(fineNumber, errorLine);
return erroredLines;
}
public Dictionary addFineStodictionary(字符串fineNumber,字符串errorLine)
{
Dictionary erroredLines=新字典();
erroredLines.添加(fineNumber,errorLine);
返回错误线路;
}
这里出现的唯一问题是,只有最新的值被添加到字典中,这意味着之前添加的值被覆盖。将erroredLines作为全局范围
Dictionary<string, string> erroredLines = new Dictionary<string, string>();
foreach (var line in this.FileLines)
{
count++;
string[] bits = line.Split(',');
fineNumber = bits[0].Trim();
int length = bits.Length;
if (length == 9)
{
//other processing gets done here, code not included as its of no interest for this question
}
else
{
//AddErrorFinesToFile(line, fineNumber);
AddFinesToDictonary(fineNumber, line);
continue;
}
}
public void AddFinesToDictonary(string fineNumber, string errorLine)
{
erroredLines.Add(fineNumber, errorLine);
// return erroredLines;
}
字典错误行=新字典();
foreach(此.FileLines中的var行)
{
计数++;
字符串[]位=行。拆分(',');
fineNumber=位[0]。Trim();
int length=位。长度;
如果(长度==9)
{
//其他处理在这里完成,代码不包括在内,因为它对这个问题不感兴趣
}
其他的
{
//AddErrorFinesToFile(行、细数);
ADDFINESTODICTORIAL(细数,行);
继续;
}
}
public void addFineStodictionary(字符串fineNumber、字符串errorLine)
{
erroredLines.添加(fineNumber,errorLine);
//返回错误线路;
}
也不需要返回erroredLines字典。将erroredLines作为全局范围
Dictionary<string, string> erroredLines = new Dictionary<string, string>();
foreach (var line in this.FileLines)
{
count++;
string[] bits = line.Split(',');
fineNumber = bits[0].Trim();
int length = bits.Length;
if (length == 9)
{
//other processing gets done here, code not included as its of no interest for this question
}
else
{
//AddErrorFinesToFile(line, fineNumber);
AddFinesToDictonary(fineNumber, line);
continue;
}
}
public void AddFinesToDictonary(string fineNumber, string errorLine)
{
erroredLines.Add(fineNumber, errorLine);
// return erroredLines;
}
字典错误行=新字典();
foreach(此.FileLines中的var行)
{
计数++;
字符串[]位=行。拆分(',');
fineNumber=位[0]。Trim();
int length=位。长度;
如果(长度==9)
{
//其他处理在这里完成,代码不包括在内,因为它对这个问题不感兴趣
}
其他的
{
//AddErrorFinesToFile(行、细数);
ADDFINESTODICTORIAL(细数,行);
继续;
}
}
public void addFineStodictionary(字符串fineNumber、字符串errorLine)
{
erroredLines.添加(fineNumber,errorLine);
//返回错误线路;
}
也无需返回erroredLines字典。原因是每次向目录添加数据时,都会创建一个新目录,而不是向现有目录添加数据 您可以做出两种选择:
out
ref传递给函数out
)传递给函数(选项2):
public void addFineStodictionary(out Dictionarydict、string fineNumber、string errorLine)
{
dict.Add(fineNumber,errorLine);
}
原因是每次向目录添加数据时,都会创建一个新目录,而不是向现有目录添加数据
您可以做出两种选择:
out
ref传递给函数out
)传递给函数(选项2):
public void addFineStodictionary(out Dictionarydict、string fineNumber、string errorLine)
{
dict.Add(fineNumber,errorLine);
}
这个怎么办
Dictionary<string, string> erroredLines = new Dictionary<string, string>();
foreach (var line in this.FileLines)
{
count++;
string[] bits = line.Split(',');
fineNumber = bits[0].Trim();
int length = bits.Length;
if (length == 9)
{
//other processing gets done here, code not included as its of no interest for this question
}
else
{
erroredLines.Add(fineNumber, line);
continue;
}
}
字典错误行=新字典();
foreach(此.FileLines中的var行)
{
计数++;
字符串[]位=行。拆分(',');
fineNumber=位[0]。Trim();
int length=位。长度;
如果(长度==9)
{
//其他处理在这里完成,代码不包括在内,因为它对这个问题不感兴趣
}
其他的
{
错误行。添加(细数,行);
继续;
}
}
在foreach之后,您可以使用erroredLines字典。这个怎么样
Dictionary<string, string> erroredLines = new Dictionary<string, string>();
foreach (var line in this.FileLines)
{
count++;
string[] bits = line.Split(',');
fineNumber = bits[0].Trim();
int length = bits.Length;
if (length == 9)
{
//other processing gets done here, code not included as its of no interest for this question
}
else
{
erroredLines.Add(fineNumber, line);
continue;
}
}
字典错误行=新字典();
foreach(此.FileLines中的var行)
{
计数++;
字符串[]位=行。拆分(',');
fineNumber=位[0]。Trim();
int length=位。长度;
如果(长度==9)
{
//其他处理在这里完成,代码不包括在内,因为它对这个问题不感兴趣
}
其他的
{
错误行。添加(细数,行);
继续;
}
}
foreach之后,您可以使用erroredLines字典。不,罚款数字不一样,每个罚款都有自己的唯一数字,这些罚款实际上来自csv文件,并且它们都是唯一的。您甚至不保存重新调整的字典。在
foreach
循环中,您不再可以访问errorLines字典。您可以只做一行:dictionary fines=this.FileLines.Select(line=>new{line,bits=line.Split(','))}。其中(x=>x.bits.Length==9)。ToDictionary(x=>x.bits[0].Trim(),x=>x.line)代码>否罚款数字不一样,每个罚款都有自己的唯一数字,这些罚款实际来自csv文件,它们都是唯一的。您甚至不保存重新调整的词典。在foreach
循环中,您不再可以访问errorLines字典。您可以只执行一行:dictionary fines=this.FileLines.Select(line=>new{line,bits=line.Split(','))})。其中(x=>x.bits.Length==9)。ToDictionary(x=>x.bits[0].Trim(),x=>x.li