1

我有一个名称,值对,两个字符串都存储在字典中。

e.g.,

Name        |   Value   |
-------------------------
Dim1L9      |   10.98   |
-------------------------
Dim14L10    |   26.32   |
-------------------------   
Dim14L11    |   95.25   |
-------------------------
Dim1L10     |   9.99    |

从这张表中,我想得到 Dim1 10.98 + 9.99 = 20.97 的总和。我怎样才能做到这一点?

4

1 回答 1

1

以防万一你想得到所有名字的总和:

var regex = new Regex(@"^[a-zA-Z]+(\d+)L\d+$");
var sumByDimension = from kvp in dictionary
            let match = regex.Match(kvp.Key)
            where match.Success
            group kvp by match.Groups[1].Value into g
            select new
            {
                Dimension = g.Key,
                Sum = g.Sum(kvp => kvp.Value)
            };

输出:

{ Dimension: "1", Sum: 20.97 }
{ Dimension: "14", Sum: 121.57}
于 2017-01-05T14:22:46.457 回答