2016-10-11 9 views
0

enter image description here私は、StringList<String>の特性を持つClassを持っています。このList<Class>Dictionary<String, List<String>>に変換したいのですが、できませんでした。私はtempCollectionリスト<Class>をC#のDictionary <string、List <String>>に変換する方法はありますか?

Dictionary<string, List<string>> tempDict = new Dictionary<string, List<string>>(tempCollection.Count); 

DictionaryにtempCollectionがに持って変換する変換したい 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; 
    } 
} 

リストの集団である、

ここ
List<SerializationClass> tempCollection = new List<SerializationClass>(); 

に直接List<Class>を変換する方法tempDictに変換する?

あなたの回答を感謝します。

ありがとうございます。

Amal Raj。

+1

一意のキーを望んでいるので、それが失敗する各要素についてkeyですか.. –

答えて

5

単にIEnumerable<T>のためのLINQのToDictionary拡張メソッドを呼び出し、唯一の条件は、一意である必要があり、さもなければDictionaryは、あなたが何をしようとした何

var tempDict = tempCollection.ToDictionary(x => x.key, x => x.values); 
+0

ありがとう。私は任意のDLLの参照を追加する必要があります。私はすでにToDictionary()を試したので、私はそれを使用することができませんでした。 – Amal

+0

System.Linq、dll、およびネームスペースを使用します。 –

+0

エラーポップアップが添付されました。 @文章 – Amal

関連する問題