【问题标题】:LINQ: An item with the same key has already been addedLINQ:已添加具有相同键的项目
【发布时间】:2013-01-14 16:11:21
【问题描述】:

使用 LINQ 将数据记录加载到对象中时出现以下错误。在我添加两个额外的字段dataLevelIddataLevel 之前它正在工作。我必须做一些我不应该做的事情,因为现在它不起作用了。谁能发现我做错了什么?

{System.ArgumentException: An item with the same key has already been added.
   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)...

这是我的代码:

var groups = reader.Cast<IDataRecord>()
    .GroupBy(dr => new { ID = (int)dr["productId"] })
    .GroupBy(g => g.Key.ID);

products = (
    from productGroup in groups
    let productRow = productGroup.First().First()
    select new Product()
    {
        ID = Convert.ToString((int)productRow["productId"]),
        Name = !DBNull.Value.Equals(productRow["name"]) ? (string)productRow["name"] : "",
        OutputFormats =
        (
            from formatRow in productGroup.First()
            select new OutputFormat()
            {
                ID = !DBNull.Value.Equals(formatRow["outputFormatId"]) ? Convert.ToString(formatRow["outputFormatId"]) : "",
                Name = !DBNull.Value.Equals(formatRow["outputFormat"]) ? (string)formatRow["outputFormat"] : "",
                DataLevelID = !DBNull.Value.Equals(formatRow["dataLevelId"]) ? Convert.ToString(formatRow["dataLevelId"]) : "",
                DataLevel = !DBNull.Value.Equals(formatRow["dataLevel"]) ? (string)formatRow["dataLevel"] : ""
            }
        ).ToSerializableDictionary(p => p.ID)
    }
).ToSerializableDictionary(p => p.ID);

这是在我的数据记录中返回的数据。

是的,OutputFormat 和 Product 类只有字符串属性(包括 Id)。

更新:

我想要的是填写我的SerializableDictionary&lt;string, Product&gt; products。 它应该有 3 个产品,每个产品都应该有相应的输出格式。

public class Product
{
    public string ID { get; set; }
    public string Name { get; set; }
    public SerializableDictionary<string, OutputFormat> OutputFormats { get; set; }
}

public class OutputFormat
{
    public string ID { get; set; }
    public string Name { get; set; }
    public string DataLevelID { get; set; }
    public string DataLevel { get; set; }
}

【问题讨论】:

  • 在将它们转换为字典之前查看调试器中的集合,看看重复的值是什么。
  • 那么当你有多个具有相同ID的行时你想做什么? (例如,您有多行的 outputFormatId 为 4,多行的 productId 为 150。)
  • @Servy 你的意思是看看groups 包含什么?我调试其余部分的困难在于它一次执行所有内容。
  • 它有什么作用:'.GroupBy(dr => new { ID = (int)dr["productId"] }) .GroupBy(g => g.Key.ID);'?
  • @JonSkeet:我已经更新了我的问题,但你让我意识到我做错了什么......每个具有相同 ID 的产品都有多种输出格式,字典无法使用。用户 Sampath 也刚刚回答了这个问题。

标签: c# linq


【解决方案1】:

您正在使用字典并尝试添加具有相同键的项目。

如下所示。

Dictionary.add("key1","item1")
Dictionary.add("key2","item2")
Dictionary.add("key1","item3") << not allowed.

检查您的代码并避免这种情况。

【讨论】:

  • 谢谢。一定是这样……我的数据发生了变化,现在单个产品有多个具有相同 ID 的输出格式。
  • @capdragon 很高兴听到它有帮助!
  • 在你的 LINQ 中使用 distinct 也可以解决这个问题,除非你真的必须对重复数据提出异常,然后在数据库中修复它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-15
  • 2013-04-04
  • 2013-02-16
  • 2022-09-29
  • 2011-06-18
相关资源
最近更新 更多