将 2 个字典项聚合到一个对象中
aggregate 2 dictionary items into an object
我有一本字典,里面有评价的答案,如下:
{
{"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 的方法相反。您 select 所有符合正则表达式 ^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();
思路是先过滤掉所有不以"_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();
您应该创建一些结构来保存这些问题的值,即:
public struct Question
{
public string QuestionId { get; set; }
public string Answer { get; set; }
public string Comment { get; set; }
}
并构建 List<Question >
:
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();
demo - 比表列表
的解决方案快一点
我有一本字典,里面有评价的答案,如下:
{
{"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 的方法相反。您 select 所有符合正则表达式 ^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();
思路是先过滤掉所有不以"_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();
您应该创建一些结构来保存这些问题的值,即:
public struct Question
{
public string QuestionId { get; set; }
public string Answer { get; set; }
public string Comment { get; set; }
}
并构建 List<Question >
:
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();
demo - 比表列表
的解决方案快一点