I am trying to convert the following string:
"CN=Test,OU=ABC,OU=Company,DC=CFLA,DC=domain"
into this form (a list of string arrays):
[
{"CN" , "Test"},
{"OU" , "ABC"},
{"OU" , "Company"},
{"DC" , "CFLA"},
{"DC" , "domain"},
]
What I have tried already is converting the string into a list of dictionaries. I tried splitting the string twice. First on the basis of , and then on the basis of =.
Following is the code I used:
var result = str.Split(',')
.Select(line => line.Split('='))
.ToDictionary(b=> b[0], b=> b[1])
.ToList();
But it gives me the following exception: 'System.ArgumentException: 'An item with the same key has already been added', rightly so as 'OU' and 'DC' are being repeated as keys.
What I want now is to split/convert/process this string into a List of string arrays (as shown above). Is it possible?
Or kindly guide me with an alternate solution. Thanks in advance.
P.S. I have hundreds of these strings and I only want the value of the first "OU" from every string.
ToDictionarycall, and you'll already have a list of string arrays. Although if the goal is just "to get the first OU from each string" then there are simpler approaches.