0

When I turn the application and turn on the exception setting, I get this error: 'An item with the same key has already been added.' on the line marked below. So can anyone help me how to fix this exception.

private void LoadQuickSelectFields()
    {
        List<Tuple<string, string>> QuickSelectable = new List<Tuple<string, string>>()
        {
            new Tuple<string, string>("new_activityreason", "new_hiscode")
        };

        var service = DAL.Service;

        try
        {
            foreach (var quickSelectField in QuickSelectable)
            {
                var qe = new QueryExpression(quickSelectField.Item1);
                qe.ColumnSet = new ColumnSet("new_hiscode", "new_name");
                var codes = service.RetrieveMultiple(qe).Entities;

                var values = new Dictionary<string, Tuple<Guid, string>>();
                QuickSelectFields.Add(quickSelectField.Item1, values);

                foreach (var code in codes)
//>>>>>> this line
                    values.Add(code.GetAttributeValue<string>(quickSelectField.Item2), new Tuple<Guid, string>(code.Id, code.GetAttributeValue<string>("new_name"))); 

            }
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "LoadQuickSelectFields failed!");
        }
        finally
        {
            DAL.ReleaseServiceInstance(service);
        }
    }
ASh
  • 34,632
  • 9
  • 60
  • 82
  • 4
    The error is pretty clear. An item with the same key was already added. We can't guess what `code.GetAttributeValue` does, but it seems it's returning the same value twice. – Panagiotis Kanavos Jun 06 '22 at 13:02
  • `Dictionary`s require each key to be unique. It won't let you add two items with the same key – Jonesopolis Jun 06 '22 at 13:02

0 Answers0