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

C# 将2个字典项聚合到一个对象中,c#,.net,dictionary,aggregate,C#,.net,Dictionary,Aggregate,我有一本字典,其中包含一个评估的答案,如下所示: { {"question1", "7"}, {"question1_comment", "pretty difficult"}, {"question2", "9"}, {"question2_comment", ""}, {"question3", "5"}, {"question3_comment", "Never on time"}, } 但是我需要将分数项和注释项组合成一个对象,如下所示

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

{
    {"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"},
}

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

未经测试,但可能是个好主意:

Regex r = new Regex("^question\\d$");
var result = myDict.Where(x => r.IsMatch(x.Key)).Select(x => new {
        Question = x.Key,
        Score = x.Value,
        Comment = myDict[x.Key + "_comment"]
});
该方法与DasBlinkenLight的方法相反。选择符合正则表达式
^question\d$
的所有条目(表示所有以数字结尾的条目)。对于这些条目,您将创建一个匿名类型的新实例,通过在字典中搜索适当的项来检索注释

编辑:或者,为了避免使用正则表达式,您可以先使用

myDict.Where(x => !x.Key.EndsWith("_comment"))

您可以这样做:

var res = data
    .Keys
    .Where(s => !s.EndsWith("_comment"))
    .Select(s => new[] {s, data[s], data[s+"_comment"]})
    .ToList();
IDE首先过滤掉所有不以
“\u comment”
结尾的键,然后使用这些键在结果数组中查找这两段内容

检查下面的答案

        Dictionary<string, string> objstr = new Dictionary<string, string>();
        objstr.Add("question1", "7");
        objstr.Add("question1_comment", "pretty difficult");
        objstr.Add("question2", "9");
        objstr.Add("question2_comment", "");
        objstr.Add("question3", "5");
        objstr.Add("question3_comment", "Never on time");

        var Mainobj = objstr.Where(x => x.Key.Contains("_"));
        var obj = objstr.Where(x => x.Key.Contains("_") == false);
        var final = from objl in obj
                    join Mainobjl in Mainobj
                    on objl.Key equals Mainobjl.Key.Replace("_comment", "") into matching
                    select new
                    {
                        question = objl.Key,
                        rank = objl.Value,
                        comment = matching.FirstOrDefault().Value
                    };
        var obj11 = final.ToList();
Dictionary objstr=new Dictionary();
对象添加(“问题1”、“7”);
添加(“问题1_注释”,“相当困难”);
对象添加(“问题2”、“9”);
添加(“问题2_注释”);
对象添加(“问题3”、“5”);
objstr.添加(“问题3_评论”,“从不准时”);
var Mainobj=objstr.Where(x=>x.Key.Contains(“”);
var obj=objstr.Where(x=>x.Key.Contains(“”)==false);
var最终=来自obj中的objl
在Mainobj中加入Mainobjl
在objl.Key上等于Mainobjl.Key.Replace(“\u comment”,”)为匹配
选择新的
{
问题=对象键,
等级=目标值,
comment=匹配的.FirstOrDefault().Value
};
var obj11=final.ToList();

您应该创建一些结构来保存这些问题的值,即:

public struct Question 
{
    public string QuestionId { get; set; }
    public string Answer { get; set; }
    public string Comment { get; set; }
}
并生成
列表


-比使用表列表的解决方案快一点

只需循环遍历项并构造自定义对象的新实例。问题是什么?问题是我不知道在循环浏览字典时如何组合正确的项目。有时我需要创建一个新项目,有时我需要添加到一个已创建的项目。@TomOrton正在合并两个或多个词典。这是从一本词典中合并两个条目。
var list = dict
    .Where(x => !x.Key.Contains("comment"))
    .Select(x => 
    new Question() 
    {
        QuestionId =x.Key, 
        Answer = x.Value, 
        Comment = dict.Single(y => 
            y.Key == String.Concat(x.Key,"_comment")).Value})
    .ToList();