C#-"An element with the same key was already added"

C# - "An element with the same key was already added"

我在 ASP.NET MVC 中工作,我在我的模型中被这个该死的错误阻塞(包围)"an element with the same key was already added",我完全不明白为什么我有几乎相同的代码和不同的请求在我的模型的其他方法中。

而且我不认为问题可能来自请求,因为我已经像以前一样在另一个项目中使用过她(它)。

public Dictionary<string,string> getDonnee()
{
    Dictionary<string, string> list = new Dictionary<string, string>();

    SqlConnection cn;
    SqlDataAdapter da;
    DataSet ds;

    cn = new SqlConnection(CS_DW);
    cn.Open();

    da = new SqlDataAdapter("select distinct(ltrim(rtrim(cpic))) as code, cpic as lib from [DW].[dbo].[PIC_PROD_S001] ", cn);
    ds = new DataSet();
    da.Fill(ds, "code");

    list.Add("REEL", "REEL");
    foreach (DataRow row in ds.Tables["code"].Rows)
    {
        list.Add(row["code"].ToString(), row["lib"].ToString());
    }

    cn.Close();
    return list;
 }

也许你可以测试一下它们是否已经在你的字典中输入了

if (!list.ContainsKey(row["code"].ToString())) 
{ 
    list.Add(row["code"].ToString(), row["lib"].ToString());
}