-1

I have a function that converts a string to a dictionary using the code below. I need to add a 3rd delimiter that is essential a row or record delimiter. Currently "," delimits [attribute,value] and "|" delimits each pair. I can't figure out how to add the 3rd delimiter to separate the rows and add them to my dictionary.

                 var dict = feed.Split(',')
                   .Select(x => x.Split('|'))
                   .ToDictionary(x => x[0], x => x[1]);
GoBeavs
  • 499
  • 2
  • 10
  • 25
  • 4
    Show what the input looks like - we can't tell you how to parse it without seeing the input. – Reed Copsey Sep 18 '12 at 21:59
  • Dictionaries have keys and values - it's not at all clear what you've really got, or what you'd want the dictionary contents to be... – Jon Skeet Sep 18 '12 at 22:02
  • If you have a row that contains name/value pairs, how do you represent the rows? Is it a List<> of Dictionary<> objects? – David Hoerster Sep 18 '12 at 22:03
  • The input will look like this 'new_field1,value1| new_field2,value2/new_field1,value1| new_field2,value2.....' – GoBeavs Sep 18 '12 at 22:34

2 Answers2

1

With '/' as the row delimiter

var dict = x.Split('/').SelectMany(s => s.Split('|')).ToDictionary(t => t.Split(',')[0], t => t.Split(',')[1]);
estebane97
  • 1,138
  • 2
  • 7
  • 25
  • This is the actual input string I tried your code with newline as the first delimiter and it's close but I get a "index out of bounds" var dict = feed.Split('\n').SelectMany(s => s.Split('|')).ToDictionary(t => t.Split(',')[0], t => t.Split(',')[1]); – GoBeavs Sep 18 '12 at 22:54
  • The input "new_permanent|False,new_rcn|55426,new_Actionid|4f9d283c-cce7-e111-b08a-000c29c8eb0f,createdon|8/16/2012 6:00:23 PM,new_primary|True,new_id|1,new_processtype|Microsoft.Xrm.Sdk.OptionSetValue,ownerid|Microsoft.Xrm.Sdk.EntityReference,modifiedon|8/20/2012 5:07:25 – GoBeavs Sep 18 '12 at 22:56
  • This worked var dict = x.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None).SelectMany(s => s.Split('|')).ToDictionary(t => t.Split(',')[0], t => t.Split(',')[1]); – GoBeavs Sep 18 '12 at 23:03
0

Line breaks are typically used to indicate new records, like with CSV files. Really, any special character or sequence will do.

Scott
  • 1,876
  • 17
  • 13