Ama*_*mal 0 c# windows string dictionary list
我Class用的性质String和List<String>.我想将其转换List<Class>为a Dictionary<String, List<String>>,但我无法做到这一点.如何List<Class>直接转换为Dictionary?
public class SerializationClass
{
string key;
List<string> values;
public SerializationClass()
{
}
public string Key
{
get
{
return this.key;
}
set
{
this.key = value;
}
}
public List<string> Values
{
get
{
return this.values;
}
set
{
this.values = value;
}
}
public SerializationClass(string _key, List<string> _values)
{
this.Key = _key;
this.Values = _values;
}
}
Run Code Online (Sandbox Code Playgroud)
列表的人口是,
List<SerializationClass> tempCollection = new List<SerializationClass>();
Run Code Online (Sandbox Code Playgroud)
在这里,我想转换为转换tempCollection为Dictionary
Dictionary<string, List<string>> tempDict = new Dictionary<string, List<string>>(tempCollection.Count);
Run Code Online (Sandbox Code Playgroud)
tempCollection必须转换为tempDict吗?
感谢您的回复.
感谢致敬,
Amal Raj.
简单地调用Linq的ToDictionary扩展方法IEnumerable<T>,只有条件是key每个元素需要是唯一的,否则它将失败,因为Dictionary想要一个唯一的密钥
var tempDict = tempCollection.ToDictionary(x => x.key, x => x.values);
Run Code Online (Sandbox Code Playgroud)