我有两个词典列表
List<Dictionary<string, string>> list1 = new List<Dictionary<string, string>>();
List<Dictionary<string, string>> list2 = new List<Dictionary<string, string>>();
List<Dictionary<string, string>> result = new List<Dictionary<string, string>>();
list1= [ { ID="3", Date="12/28/2013", Target="1",...},
{ ID="4", Date="12/30/2013", Target="33",... }]
list2=[ { ID="3", ASO="100", Below="50"},
{ ID="4", ASO="40", Below="33" }]
现在我希望结果是
result= [ { ID="3", Date="12/28/2013", Target="1", ASO="100", Below="50",...},
{ ID="4", Date="12/30/2013", Target="33", ASO="40", Below="33",... }]
结果在list1 id = list2 id
上形成。
上面的代码就像想法一样
这是我的完整代码
List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();
List<Dictionary<string, string>> list2 = new List<Dictionary<string, string>>();
List<Dictionary<string, string>> listresult = new List<Dictionary<string, string>>();
var summary = (from a in _context.tblReportProfessions
where a.tblBRReport.ID==4 || a.tblBRReport.ID==5||a.tblBRReport.ID==3
select new
{
ID = a.tblBRReport.ID,
DateOf = a.tblBRReport.ReportDate.ToShortDateString(),
Target = a.tblBRReport.frnchiseTarget,
Name=a.tblCampaignProfession.tblAllProfession.ProfessionName,
Qty=a.NoOfConsumer
})
.ToList();
var summary2 = (from a in _context.tblReportAges
where a.tblBRReport.ID == 4 || a.tblBRReport.ID == 5 || a.tblBRReport.ID == 3
select new
{
ID = a.tblBRReport.ID,
Name = a.tblCampaignAge.StartAge + "-" + a.tblCampaignAge.EndAge,
Qty = a.NoOfConsumer
})
.ToList();
int id = 0;
Dictionary<string, string> result = new Dictionary<string, string>();
foreach (var ex in summary)
{
if (id == ex.ID)
{
result[ex.Name.ToString()] = ex.Qty == null ? "" : ex.Qty.ToString();
}
else
{
if (id != 0)
list.Add(result);
id = ex.ID;
result = new Dictionary<string, string>();
result["ID"] = ex.ID.ToString();
result["Date"] = ex.DateOf;
result["Target"] = ex.Target.ToString();
result[ex.Name.ToString()] = ex.Qty == null ? "" : ex.Qty.ToString();
}
}
list.Add(result);
int id2 = 0;
Dictionary<string, string> result2 = new Dictionary<string, string>();
foreach (var ex in summary2)
{
if (id2 == ex.ID)
{
result2[ex.Name.ToString()] = ex.Qty == null ? "" : ex.Qty.ToString();
}
else
{
if (id2 != 0)
list2.Add(result2);
id2 = ex.ID;
result2 = new Dictionary<string, string>();
result2["ID"] = ex.ID.ToString();
result2[ex.Name.ToString()] = ex.Qty == null ? "" : ex.Qty.ToString();
}
}
list2.Add(result2);
现在我希望listresult=list.Join(list2)
如何做到这一点.id是常用密钥
`如何合并两个字典列表?任何帮助我的人都非常感激。
编辑:列出数据(来自Firebug的Json格式)
0
Object { ID="3", Date="12/28/2013", Target="1", more...}
Business
""
Date
"12/28/2013"
Driver
"33"
Farmer
"33"
ID
"3"
Labor
""
Others
""
Puller
"33"
Service
"33"
Student
""
Target
"1"
Unemployed
""
1
Object { ID="4", Date="12/30/2013", Target="33", more...}
Business
"33"
Date
"12/30/2013"
Driver
""
Farmer
""
ID
"4"
Labor
""
Others
""
Puller
""
Service
""
Student
"33"
Target
"33"
Unemployed
"33"
list2数据
0
Object { ID="3", 18-25="33", 26-30="33", more...}
18-25
"33"
26-30
"33"
31-35
"33"
ASO-35
"33"
ID
"3"
1
Object { ID="4", 18-25="33", 26-30="33", more...}
18-25
"33"
26-30
"33"
31-35
"33"
ASO-35
"33"
ID
"4"
我想基于list和list2的ID加入并将结果发送到浏览器。
答案 0 :(得分:2)
提供的数据不是List<Dictionary<string, string>>
。但是使用匿名类型,您可以尝试以下方法:
var list1 = new []
{
new {ID = "3", Date = "12/28/2013", Target = "1"},
new {ID = "4", Date = "12/30/2013", Target = "33"}
};
var list2 = new[]
{
new {ID = "3", ASO = "100", Below = "50"},
new {ID = "4", ASO = "40", Below = "33"}
};
var result = list1
.Join(
list2,
arg1 => arg1.ID,
arg2 => arg2.ID,
(arg1, arg2) => new
{
arg1.ID,
arg1.Date,
arg1.Target,
arg2.ASO,
arg2.Below
})
.ToArray();
但似乎你需要一些自定义类型。
答案 1 :(得分:1)
以下是List包含词典的解决方案:
示例设置
var list = new List<Dictionary<string, string>> {
new Dictionary<string, string> { { "ID", "3" }, { "Date", "12/28/2013" }, { "Target", "1" }},
new Dictionary<string, string> { { "ID", "4" }, { "Date", "12/30/2013" }, { "Target", "1" }}
};
var list2 = new List<Dictionary<string, string>> {
new Dictionary<string, string> { { "ID", "3" }, { "ASO", "100" }, { "Below", "50" } },
new Dictionary<string, string> { { "ID", "4" }, { "ASO", "40" }, { "Below", "33" } }
};
这是您感兴趣的内容
var result = from first in list
join second in list2
on first["ID"] equals second["ID"]
select new { ID = first["ID"], Date = first["Date"], Target = first["Target"], ASO = second["ASO"], Below = second["Below"]};
它给你这个(完全是你想要的结果)
ID Date Target ASO Below
3 12/28/2013 1 100 50
4 12/30/2013 1 40 33
答案 2 :(得分:1)
附件是一个自定义类型的解决方案:
ForEach循环显然可以转换为LINQ,但不是为了提高可读性。
public class CustomObject
{
public string ID { get; set; }
public string Date { get; set; }
public string Target { get; set; }
public string ASO { get; set; }
public string Below { get; set; }
}
public class CustomObjectMerger
{
public List<CustomObject> Merge()
{
List<CustomObject> list1 = new List<CustomObject>();
List<CustomObject> list2 = new List<CustomObject>();
List<CustomObject> result = new List<CustomObject>();
list1.Add(new CustomObject { ID = "3", Date = "12/28/2013", Target = "1" });
list1.Add(new CustomObject { ID = "4", Date = "12/30/2013", Target = "33" });
list2.Add(new CustomObject { ID = "3", ASO = "100", Below = "50" });
list2.Add(new CustomObject { ID = "4", ASO = "40", Below = "33" });
foreach (CustomObject customObject1 in list1)
{
foreach (CustomObject customObject2 in list2)
{
if (customObject1.ID == customObject2.ID)
{
result.Add(new CustomObject { ID = customObject1.ID, ASO = customObject2.ASO, Below = customObject2.Below, Target = customObject1.Target, Date = customObject1.Date });
}
}
}
return result;
}
}
答案 3 :(得分:1)
为什么你有一本字典列表?这确实使事情复杂化,但仍然可以做你想做的事情。我觉得你应该使用两个List<T>
。
您的示例不完整 - 它缺少您的自定义类型,因此很难准确确定您要执行的操作。但是,如果您正在使用自定义类型,则通常需要第三种复合类型来将结果投影到 - 我在这个示例中使用了第二种类型。
但是,我认为你要做的就是小组和项目。我刚刚将这些内容混合在一起向您展示,假设您的原始代码中确实存在自定义类型,并且您希望对ID字段进行分组(同样,假设)。我把它分解成了几段,所以它更具可读性:
public void PerformTest() {
var list1 = new List<Dictionary<string, MyCustomType>>();
var list2 = new List<Dictionary<string, MyCustomType2>>();
list1 = new List<Dictionary<string, MyCustomType>> {
new Dictionary<string, MyCustomType> {
{
"key1",
new MyCustomType {
ID = "3",
Date = "12/28/2013",
Target = "1"
}
}, {
"key2",
new MyCustomType {
ID = "4",
Date = "12/30/2013",
Target = "33"
}
}
}
};
list2 = new List<Dictionary<string, MyCustomType2>> {
new Dictionary<string, MyCustomType2> {
{
"key3",
new MyCustomType2 {
ID = "3",
Date = "12/28/2013",
Target = "1",
ASO = "100",
Below = "50"
}
}, {
"key4",
new MyCustomType2 {
ID = "4",
Date = "12/28/2013",
Target = "1",
ASO = "100",
Below = "50"
}
}
}
};
var innerList1 = list1.SelectMany(x => x.Values);
var innerList2 = list2.SelectMany(x => x.Values);
var list3 = innerList1
.Join(innerList2, x => x.ID, x => x.ID, (x, y) => new MyCustomType2 {
ASO = y.ASO,
Below = y.Below,
Date = x.Date,
ID = x.ID,
Target = x.Target
});
}
public class MyCustomType {
public string ID = "3";
public string Date = "12/28/2013";
public string Target = "1";
}
public class MyCustomType2 {
public string ID = "3";
public string Date = "12/28/2013";
public string Target = "1";
public string ASO = "100";
public string Below = "50";
}
答案 4 :(得分:0)
其实我想要以下
Dictionary<string, string> listResultitem = new Dictionary<string, string>();
foreach(var dic in list)
{
foreach (var dic2 in list2)
{
if (dic["ID"] == dic2["ID"])
{
listResultitem = dic.Concat(dic2.Where(kvp => !dic.ContainsKey(kvp.Key))).ToDictionary(d => d.Key, d => d.Value);
listresult.Add(listResultitem);
}
}
}