0

I need to split a custom string into the following format using C#.

The following string: AD=Demo,OU=WEB,OU=IT,L=MyCity,C=MyCountry, i want to split it at comma into a

List<CustomDictionary> myList = new List<CustomDictionary>();

Based on the text above and after the split, the myList list should contain 5 objects of type CustomDictionary.

 object1.Key = AD
 object1.Value = Demo

 object2.Key = OU
 object2.Value = WEB

 object3.Key = OU
 object3.Value = IT

 object4.Key = L
 object4.Value = MyCity

 object5.Key = C
 object5.Value = MyCountry

Here is the CustomObject class

 public class CustomDictionary
 {
     public string Key { get; set; }
     public string Value { get; set; }

     public CustomDictionary(string key, string value)
     {
         this.Key = key;
         this.Value = value;
     }
 }

So far I tried this:

Here I am stuck!

  List<CustomDictionary> keyVal = new List<CustomDictionary>val.Split(',').Select(x=>x.Split('=')).Select(x=>x.));

where val is the actual string ...

5
  • Any reason you can't us the built in Active Directory classes? Commented Jan 12, 2013 at 17:47
  • 1
    Also, what have you tried? Please post your current code and explain where you are stuck. Commented Jan 12, 2013 at 17:48
  • @Oded the string is just a value I receive in the service call ... Commented Jan 12, 2013 at 17:53
  • I could have split this to a keyvalue pair (Dictionary), but I will have duplicate Key than, unless I will reverse the values but even than I am not sure I will ever receive non-duplicate values so I have to figure this out in some other way ... Commented Jan 12, 2013 at 17:55
  • 1
    So, what's wrong with a List<KeyValuePair>? It is not a dictionary. Commented Jan 12, 2013 at 17:59

7 Answers 7

5

With linq:

var query = from x in str.Split(',')
            let p = x.Split('=')
            select new CustomDictionary(p[0], p[1]);

var list = query.ToList();

Also seems like you want to get a dictionary as a result. If so, try this code:

var dict = str.Split(',').Select(x => x.Split('='))
                         .ToDictionary(x => x[0], x => x[1]);

To handle duplicate keys, you can store objects in Lookup. Just call ToLookup instead of ToDictionaty.

Sign up to request clarification or add additional context in comments.

1 Comment

I made it at first with Dictionary but when I run the code, i got an exception because I have duplicate keys: OU ... so this was a lost. however it works fine with the first linq query you shown ...
2

After splitting the second time you create a CustomDictionary from the items in that array, then use ToList to make a list of the result.

List<CustomDictionary> keyVal =
  val.Split(',')
  .Select(x => x.Split('='))
  .Select(a => new CustomDictionary(a[0], a[1]))
  .ToList();

There is already a class in the framework having a key and value, which you can use instead:

List<KeyValuePair<string, string>> keyVal =
  val.Split(',')
  .Select(x => x.Split('='))
  .Select(a => new KeyValuePair<string, string>(a[0], a[1]))
  .ToList();

You can also use a Dictionary<string, string> instead of a list of key-value pairs. It stores the value based on the hash code of the key, so getting a value by key is much faster than looking through a list (but it doesn't retain the order of the items):

Dictionary<string, string> keyVal =
  val.Split(',')
  .Select(x => x.Split('='))
  .ToDictionary(a => a[0], a => a[1]);

Comments

1

This is how you would do it:

var parts = theString.Split(',');

var myList = new List<CustomDictionary>();
foreach(string part in parts)
{
  var kvp = part.Split('=');
  myList.Add(new CustomDictionary(kvp[0], kvp[1]));
}

This can also be done using LINQ.

Comments

1

Since you have 2 OUs you can't use Dictionary. Instead use Lookup

string input = "AD=Demo,OU=WEB,OU=IT,L=MyCity,C=MyCountry";

var dict = Regex.Matches(input, @"(\w+)=([^,$]+)").Cast<Match>()
            .ToLookup(m => m.Groups[1].Value, m => m.Groups[2].Value);

Comments

0

what about MyString.split(','); and the on each string you get:

CO.key = SubString.split('=')[0];
CO.value = SubString.split('=')[1];

Comments

0

With LINQ:

List<CustomDictionary> myList = (from x in input.Split(new char[] { ',' })
                                 select
                                 new CustomDictionary (x.Substring(0, x.IndexOf('=')), x.Substring(x.IndexOf('=') + 1))
                                ).ToList();

Comments

0
string str = "AD=Demo,OU=WEB,OU=IT,L=MyCity,C=MyCountry";

var result = str.Split(',').Select(s =>
{
    var tmp = s.Split('=');
    return new CustomDictionary(tmp[0], tmp[1]);
}).ToList();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.