For future reference I want to document the above code converted to using a declarative approach as a LinqPad code snippet:
var sourceString = @"<3>
<1>
<8>";
var count = 0;
var ItemRegex = new Regex(@"<(?- [^>]+)><(?[^>]*)>", RegexOptions.Compiled);
var OrderList = ItemRegex.Matches(sourceString)
.Cast()
.Select(m => new
{
Name = m.Groups["item"].ToString(),
Count = int.TryParse(m.Groups["count"].ToString(), out count) ? count : 0,
})
.ToList();
OrderList.Dump();
With output:
