C# 如何为dictionary对象中的同一个键创建一个值集合?

C# 如何为dictionary对象中的同一个键创建一个值集合?,c#,dictionary,C#,Dictionary,我有一个实体,如下所示 public class ContextElements { public string Property { get; set; } public string Value { get; set; } } 现在我填充了实体,如下所示(它模拟了来自web服务的实际输入) var集合=新列表(); 添加(新ContextElements{Property=“Culture”,Value=“en-US”}); 添加(新ContextEle

我有一个实体,如下所示

 public class ContextElements
{
        public string Property { get; set; }

        public string Value { get; set; }
}
现在我填充了实体,如下所示(它模拟了来自web服务的实际输入)

var集合=新列表();
添加(新ContextElements{Property=“Culture”,Value=“en-US”});
添加(新ContextElements{Property=“Affiliate”,Value=“0”});
添加(新ContextElements{Property=“EmailAddress”,Value=”sreetest@test.com" });
添加(新ContextElements{Property=“Culture”,Value=“fr”});
添加(新ContextElements{Property=“Affiliate”,Value=“1”});
添加(新ContextElements{Property=“EmailAddress”,Value=”somemail@test.com" });
现在我有一个dictionary对象,如下所示

Dictionary<string, List<string>> dictStr = new Dictionary<string, List<string>>();
Dictionary dictStr=new Dictionary();
我要寻找的输出是,对于这里的每个dictionc键(即属性),例如“Culture”、“Affiliate”、“EmailAddress”,值都将出现在列表集合中

i、 e.字典的最终输出将是以下内容的输出(显然是在运行时和编程时)

添加(“文化”,新列表(){“en-US”,“fr-fr”}); 添加(“附属”,新列表(){“0”,“1”}); 添加(“电子邮件地址”,新列表(){”sreetest@test.com", "somemail@test.com" }); 需要帮助

谢谢

如果您可以使用LINQ(.NET 3.5及以上版本),您可以:

Dictionary<string, List<string>> dictStr = collection.GroupBy(x => x.Property)
          .ToDictionary(x => x.Key, x => x.Select(y => y.Value).ToList());
Dictionary dictStr=collection.GroupBy(x=>x.Property)
.ToDictionary(x=>x.Key,x=>x.Select(y=>y.Value).ToList());
如果您可以使用LINQ(.NET 3.5及更高版本),您可以:

Dictionary<string, List<string>> dictStr = collection.GroupBy(x => x.Property)
          .ToDictionary(x => x.Key, x => x.Select(y => y.Value).ToList());
Dictionary dictStr=collection.GroupBy(x=>x.Property)
.ToDictionary(x=>x.Key,x=>x.Select(y=>y.Value).ToList());
var dictStr=new Dictionary();
foreach(集合中的var元素)
{
列表值;
如果(!dictStr.TryGetValue(element.Property,out value))
{
值=新列表();
dictStr.Add(element.Property,value);
}
Value.Add(element.Value);
}
var dictStr=new Dictionary();
foreach(集合中的var元素)
{
列表值;
如果(!dictStr.TryGetValue(element.Property,out value))
{
值=新列表();
dictStr.Add(element.Property,value);
}
Value.Add(element.Value);
}

我相信肖邦的解决方案会管用,但对于IEnumerable不能转换为字典第二个通用参数列表的小问题。请尝试以下方法:

collection.GroupBy(x => x.Property).ToDictionary(x => x.Key, x => x.Select(y => y.Value).ToList());

我相信肖邦的解决方案会奏效,但对于IEnumerable不能转换为你字典第二个通用参数列表的小问题。请尝试以下方法:

collection.GroupBy(x => x.Property).ToDictionary(x => x.Key, x => x.Select(y => y.Value).ToList());

如果我执行dictStr=collection.GroupBy(x=>x.Property).ToDictionary(x=>x.Key,x=>x.Select(y=>y.Value));获取错误1无法将类型“System.Collections.Generic.Dictionary”隐式转换为“System.Collections.Generic.Dictionary”,如果我执行dictStr=collection.GroupBy(x=>x.Property).ToDictionary(x=>x.Key,x=>x.Select(y=>y.Value));获取错误1无法将类型“System.Collections.Generic.Dictionary”隐式转换为“System.Collections.Generic.Dictionary”
collection.GroupBy(x => x.Property).ToDictionary(x => x.Key, x => x.Select(y => y.Value).ToList());