我有下面的词典:
Dictionary<string, Value> DictFoundExpected = new Dictionary<string, Value>();
public struct Value
{
string code;
int found;
int expected;
public Value(int f,int exp,string code)
{
this.found=f;
this.expected=exp;
this.code = code;
}
}
脚本:
while (sqlreader.Read())
{
if (DictFoundExpected.ContainsKey(sqlreader[1].ToString()))
{
DictFoundExpected[sqlreader[1].ToString()]=?????// I want to increase Value.expected by 1
}
else
{
DictFoundExpected.Add(sqlreader[1].ToString(),new Value(0,0,sqlreader[0].ToString()));
}
}
我坚持如何增加Value.expected
答案 0 :(得分:2)
我坚持如何增加Value.expected
的值
只需在struct value
添加一个方法,即增加Value.expected
,如:
public struct Value
{
string code;
int found;
int expected;
public void IncreaseExpected()
{
expected++;
}
public Value(int f, int exp, string code)
{
this.found = f;
this.expected = exp;
this.code = code;
}
}
并使用它(我稍微更改了代码,只读了sqlreader field
一次):
while (sqlreader.Read())
{
String currentCode = sqlreader[1].ToString();
if (DictFoundExpected.ContainsKey(currentCode))
{
DictFoundExpected[currentCode].IncreaseExpected();
}
else
{
DictFoundExpected.Add(currentCode,new Value(0,0,currentCode));
}
}
答案 1 :(得分:1)
while (sqlreader.Read())
{
string key = sqlreader[1].ToString();
if (DictFoundExpected.ContainsKey(key))
{
Value v = DictFoundExpected[key];
DictFoundExpected[key]= new Value(v.Found, v.Expected + 1, v.Code);
}
else {
DictFoundExpected.Add(key,new Value(0,0,sqlreader[1].ToString()));
}
}
这假设您的Value结构具有以下属性:
public int Found { get { return found; } }
public int Expected { get { return expected; } }
public string Code { get { return code; } }