So I'm trying to solve this issue, which shouldn't be too hard, but I'm stuck on it for far too long now.
This is the data I'm working with var data = "2,6;2,7;4,14;5,20";
It's a string that shows <modifierGroup>,<modifier>;<modifierGroup>,<modifier>;...
This is the model I eventually want to get my data in:
public class ModifierGroup
{
public int Id { get; set; }
public List<Modifier> Modifiers { get; set; }
}
public class Modifier
{
public int Id { get; set; }
}
Right now I keep thinking I need to get my data in this format, so I can eventually push it into the model:
Key=2
Value=6
Value=7
Key=4
Value=14
Key=5
Value=20
But I could be wrong. I'd love to keep the code short. So I'd rather prevent loops in loops and doing if statements over and over. Best case scenario I get a 1 or 2-liner of code, but if it doesn't work, it doesn't work.
;that will give you array of string. Use ForEach Linq to process each item in the array. Each item in the array can be further split by,. Add the item in the dictionary with the first part of the string is key (modifiergroup) and nextone is value(modifier).var matches = Regex.Matches(data, @"(?<=(^|;))(?<key>\d+),(?<value>\d+)(;|$)").Select(m => new { key = m.Groups["key"].Value, value = m.Groups["value"].Value })