将2个字典项聚合成一个对象

use*_*279 4 .net c# dictionary aggregate

我有一本包含评估答案的字典,如下所示:

{
    {"question1", "7"},
    {"question1_comment", "pretty difficult"},
    {"question2", "9"},
    {"question2_comment", ""},
    {"question3", "5"},
    {"question3_comment", "Never on time"},
}
Run Code Online (Sandbox Code Playgroud)

但是我需要将得分项和评论项组合成一个对象,如下所示

{
    {"question1", "7", "pretty difficult"},
    {"question2", "9", ""},
    {"question3", "5", "Never on time"},
}
Run Code Online (Sandbox Code Playgroud)

我想我需要使用Aggregate方法来解决这个问题,但我不知道从哪里开始.

das*_*ght 5

你可以这样做:

var res = data
    .Keys
    .Where(s => !s.EndsWith("_comment"))
    .Select(s => new[] {s, data[s], data[s+"_comment"]})
    .ToList();
Run Code Online (Sandbox Code Playgroud)

ides首先过滤掉所有未结束的键,"_comment"然后使用这些键在结果数组中查找两段内容.

演示.