This is related to a previous question, but it seems different enough to post it separately:
I have settings that are stored in string arrays from user input (DomAttributeIds and IntlAttributeIds). I am trying to conditionally build an array of values to execute some code on. Conditions are:
- If
DomAttributesSettingsexist, create an array of the values and pass them to theAddAttributeEdit()method. - If
IntlAttributesSettingsexist, create an array of those settings and combine them with the settings from condition one distinctly (no duplicates) and pass that array to the rest of the code, and ultimately each array element to theAddAttributeEdit()method.
The code below seems to work except for the "no duplicates" part. I thought using Linq's Union and/or Distinct methods would do this, but I must be doing something wrong. My code throws the following exception:
Multiple controls with the same ID 'trAttribute_493' were found. FindControl requires that controls have unique IDs.
I know that this is because that id exists in both settings. The AddAttributeEdit() method at the end contains code that builds a table and cells with Ids based on the values passed to it. What am I doing wrong to get a distinct union of the two arrays?
Here is the code:
private void LoadAttributes()
{
if (!string.IsNullOrEmpty(DomAttributesSetting))
{
string[] attributeIds;
if (!string.IsNullOrEmpty(IntlAttributesSetting))
{
string[] domattributeIds = DomAttributesSetting.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
string[] intlattributeIds = IntlAttributesSetting.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
IEnumerable<string> atrributeIdList = domattributeIds.Union(intlattributeIds).Distinct();
attributeIds = atrributeIdList.ToArray();
}
else
{
attributeIds = DomAttributesSetting.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
}
foreach (string attributeId in attributeIds)
{
int attrId = -1;
if (int.TryParse(attributeId, out attrId) && attrId != -1)
{
Arena.Core.Attribute attribute = new Arena.Core.Attribute(attrId);
PersonAttribute personAttribute = (PersonAttribute)person.Attributes.FindByID(attribute.AttributeId);
if (personAttribute == null)
personAttribute = new PersonAttribute(person.PersonID, attrId);
AddAttributeEdit(attribute, personAttribute, true);
}
}
}
}...