gpt4 book ai didi

c# - 将 2 个字典项聚合到一个对象中

转载 作者:太空狗 更新时间:2023-10-30 01:02:48 24 4
gpt4 key购买 nike

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

{
{"question1", "7"},
{"question1_comment", "pretty difficult"},
{"question2", "9"},
{"question2_comment", ""},
{"question3", "5"},
{"question3_comment", "Never on time"},
}

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

{
{"question1", "7", "pretty difficult"},
{"question2", "9", ""},
{"question3", "5", "Never on time"},
}

我想我需要使用聚合方法来实现这一点,但我不知道从哪里开始。

最佳答案

你可以这样做:

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

思路是先过滤掉所有不以"_comment"结尾的键,然后用这些键将这两段内容查找到结果数组中。

Demo.

关于c# - 将 2 个字典项聚合到一个对象中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32375424/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com