如何在C#中将List <Class>转换为Dictionary <string,List <String >>?

Ama*_*mal 0 c# windows string dictionary list

在此输入图像描述Class用的性质StringList<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)

在这里,我想转换为转换tempCollectionDictionary

Dictionary<string, List<string>> tempDict = new Dictionary<string, List<string>>(tempCollection.Count);
Run Code Online (Sandbox Code Playgroud)

tempCollection必须转换为tempDict吗?

感谢您的回复.

感谢致敬,

Amal Raj.

Mri*_*boj 5

简单地调用Linq的ToDictionary扩展方法IEnumerable<T>,只有条件是key每个元素需要是唯一的,否则它将失败,因为Dictionary想要一个唯一的密钥

var tempDict = tempCollection.ToDictionary(x => x.key, x => x.values);
Run Code Online (Sandbox Code Playgroud)