Reputation: 5742
foreach (Benefit bene in claim.SubClaimFolderCollection[0].BenefitsFolder.BenefitCollection)
{
Dictionary<string, object> searchBeneficiary =
new Dictionary<string,object>();
searchBeneficiary.Add("rli_beneficiariesid", ((Guid)claimant.GetValue("rli_subclaimfolderid", true)));
}
It gives me this error
An item with the same key has already been added while adding an item to dictionary.
Upvotes: 2
Views: 10086
Reputation: 1559
This code cannot lead to the error. :)
Please, post the code of claimant.GetValue(...) method and from methods/properties it access from inside (if any). I suppose, that you may save smth inside this method to some other dictionary.
Upvotes: 0
Reputation: 27618
It looks to me like the "rli_beneficiariesid" key is being added twice to the dictionary each time through the loop:
(Code copied and pasted from current code sample in original post):
searchBeneficiary.Add(
"rli_beneficiariesid",
searchBeneficiary.Add("rli_beneficiariesid", ((Guid)claimant.GetValue("rli_subclaimfolderid", true)));
Note that searchBeneficiary.Add is being called with "rli_beneficiariesid". Inside of the Add call, searchBeneficiary.Add is being called again with the same key.
The net effect seems to be that a guid (claimant.GetValue) is being added to the searchBeneficiary dictionary with the "rli_beneficiariesid" key and then the dictionary is being added to itself with the same key!
Upvotes: 2
Reputation: 1559
It seems that error cannot appear here: addition of a single pair to an empty Dictionary... Please, give a full stack trace from the exception.
Upvotes: 0
Reputation: 839074
Are you sure that the error is coming from here? It looks like for each iteration you are creating a new dictionary, inserting a single entry into it, then discarding the dictionary. That shouldn't give this error, and it's also very unlikely to be what you intended to do.
If you want to convert a collection to a Dictionary I'd suggest using the Enumerable.ToDictionary method (requires .NET 3.5).
Upvotes: 7