C# 键值对如何处理与已存在的键的精确匹配?

C# 键值对如何处理与已存在的键的精确匹配?,c#,keyvaluepair,C#,Keyvaluepair,我有一个要插入字典的集合,并且有一些值已经输入到KVPs中。假设我已经有了一个“4000”键,值又出现了,值会发生什么变化?它是否将密钥实例的值添加到该密钥已经存在的值中?它是否覆盖了该值 如果它进行了重写,我如何在值遍历值集合时添加这些值 public class AccountBalance { public decimal balance { get; set; } public bool balancesheetact { get; set; } public

我有一个要插入字典的集合,并且有一些值已经输入到KVPs中。假设我已经有了一个“4000”键,值又出现了,值会发生什么变化?它是否将密钥实例的值添加到该密钥已经存在的值中?它是否覆盖了该值

如果它进行了重写,我如何在值遍历值集合时添加这些值

public class AccountBalance
{
    public decimal balance { get; set; }
    public bool balancesheetact { get; set; }

    public AccountBalance()
    {
        balance = 0;
        balancesheetact = false;
    }

}

Dictionary<string, List<AccountBalance>> balances = new Dictionary<string, List<AccountBalance>>();
var jentries = from je in gl.JEHeaders
               where je.ped_int_id >= beginperiod && je.ped_int_id <= endperiod
               orderby je.ped_int_id
               select je;

bool isBalanceSheet;

foreach (JEHeader header in jentries)
{
    foreach (JEDetail entry in header.JEDetails)
    {
        string subAccount = entry.ChartOfAccounts.acc_ext_id.Trim();
        string key = subAccount.Remove(0, 4);

        if (entry.ChartOfAccounts.acc_ty >= 15320 && entry.ChartOfAccounts.acc_ty <= 15322)
            isBalanceSheet = true;
        else
            isBalanceSheet = false;


        AccountBalance value = null;

        if (!balances.ContainsKey(key))
        {
            List<AccountBalance> account_balances = new List<AccountBalance>();
            //   for (int i = 0; i < 12; ++i)
            for (int i = 0; i < 14; ++i)
                account_balances.Add(new AccountBalance());

            balances.Add(key, account_balances);
        }

        //   value = balances[key][header.ped_int_id % beginperiod];
        value = balances[key][(header.ped_int_id % beginperiod) + 1];


        /// NEW
        if (header.ped_int_id == 637)
            value = balances[key][0];
        ///  end NEW

        if (entry.jnl_pst_deb_at != null)
            value.balance += entry.jnl_pst_deb_at.HasValue ? entry.jnl_pst_deb_at.Value : 0;
        if (entry.jnl_pst_crd_at != null)
            value.balance -= entry.jnl_pst_crd_at.HasValue ? entry.jnl_pst_crd_at.Value : 0;

        if (isBalanceSheet == true)
            value.balancesheetact = true;
        else
            value.balancesheetact = false;


    }
}

balances = balances.OrderBy(kvp => kvp.Key).ToDictionary(xyz => xyz.Key, xyz => xyz.Value);
int row = 0;
decimal ytdbalance;
foreach (KeyValuePair<string, List<AccountBalance>> account in balances)
{
    row++;
    //string subAccount = account.Key.Remove(0, 4);
    workbook.AddCell(row, 0, account.Key);
    ytdbalance = 0;

    bool BS = account.Value[0].balancesheetact;

    for (int i = 1; i < 13; ++i)
    {
        ytdbalance = ytdbalance + account.Value[i].balance;
        if (BS == true)
            workbook.AddCell(row, i + 1, ytdbalance);
        else
            workbook.AddCell(row, i + 1, account.Value[i].balance);

    }

}
公共类账户余额
{
公共十进制余额{get;set;}
公共布尔平衡表{get;set;}
公共帐户余额()
{
余额=0;
平衡表=假;
}
}
字典余额=新字典();
var jentries=来自gl.JEHeaders中的je
其中je.ped_int_id>=beginperiod&&je.ped_int_id=15320&&entry.chartofcounts.acc_ty kvp.Key)。ToDictionary(xyz=>xyz.Key,xyz=>xyz.Value);
int行=0;
十进制平衡;
foreach(余额中的KeyValuePair账户)
{
行++;
//string subAccount=account.Key.Remove(0,4);
workbook.AddCell(第0行,account.Key);
YTDBANCE=0;
bool BS=account.Value[0]。余额单;
对于(int i=1;i<13;++i)
{
ytdbalance=ytdbalance+科目.值[i].余额;
如果(BS==真)
工作簿.AddCell(行,i+1,ytdbalance);
其他的
workbook.AddCell(行,i+1,account.Value[i].余额);
}
}

字典是1对1映射,如果需要1对多映射,可以创建一个


复制到字典会导致错误。

它确实会覆盖。如果您想在现有的基础上增加价值,我相信您正在寻找:

if (dict.ContainsKey(key)) {
    dict[key] += newVal;
} else {
    dict[key] = newVal;
}

// or

int currentVal;
dict.TryGetValue(key, out currentVal);
dict[key] = currentVal + newVal;
您必须首先检查它是否存在,如果存在,则添加值并重新插入字典。如果它不存在,可以将其设置为正常


注意此处的线程安全性-如果您的字典是通过多个线程访问的,则此代码可能会使您的字典出错。

如果您只想在执行过程中添加值,那么就不要说
字典
,而是要字典
,而不要只调用add之类的命令

if (!myDict.ContainsKey(someKey))
{
   myDict.Add(someKey,new List<String>());
}
myDict[somekey].Add(somevalue);
if(!myDict.ContainsKey(someKey))
{
Add(someKey,newlist());
}
myDict[somekey].Add(somevalue);

无论如何,这是一种方法

字典不能有重复的键。您可以添加示例源代码吗?您可能需要一个
MultiMap
aka
MultiDictionary
,它可以用同一个键处理多个项目。如果是这样,请在此处查看答案:或者,您可以使用
字典
(如此处所述:)添加的代码。很抱歉,这看起来很可怕。“我永远无法得到正确的格式。@SLaks-我没有说字典有重复的键。”。我问字典是否有副本,键和值会发生什么变化。如果调用Add,它只会出错;如果调用TryAdd,它只会返回
false
。我们需要查看您添加的代码以提供更多帮助。我认为您有一些语法问题。我发现并修复了一个。谢谢,我想这就是我要找的。