I have a string like this:
var totalOrders = "Beans - 1\nMeat balls - 1\nBrown bread - 2\nBeans - 2\nBanana slice - 1\nSteak with rice - 1\nBrown bread - 1\n";
It's dynamically created, so this one is just for the sake of example. Numbers represent how many portions are ordered. I need to split it to dictionary by the name of food as a key and count how many portions are ordered which would be a value.
I've tried this for spliting the string, first by '\n' then by ' - ':
count = totalOrders.Split('\n').ToDictionary(item => item.Split(" - ")[0], item => int.Parse(item.Split(" - ")[1]));
but I get an error "cannot convert from string to char. I had to use double quotes, because " - " this has 3 chars (2 spaces and a dash) and it's not recognized as a char.
Also, I think the problem will be when I start adding food, because it will report that keys won't be unique, considering that food appears more than once. How to solve this?
EDIT:
When I try this:
string[] countOrders = totalOrders.Split('\n');
string[] parts1 = countOrders.Split(new string[] { " - " }, StringSplitOptions.None);
I get error at .Split
'string[]' does not contain a definition for 'Split' and no extension method 'Split' accepting a first argument of type 'string[]' could be found
and if I try this "string countOrders =" (so without []) I get
Cannot implicitly convert type 'string[]' to 'string'
I don't understand how am I suppose to split the string by '\n' and then to split it by " - ", when it gives me errors in both cases?