2011-10-17 6 views
1

クエリの作成に苦労しています。私はリスト<一覧<ダブル>>の値を取得する必要がありこのXMLファイルのクエリはどのようになりますか?

<Root> 
<Summary> 
    <Objective ID="1"> 
     <Exam Result="70" /> 
     <Exam Result="90" /> 
    </Objective> 
    <Objective ID="2"> 
     <Exam Result="100" /> 
     <Exam Result="90" /> 
    </Objective> 
</Summary> 
</Root> 

:これがファイルです。目標の場合は最初のリスト、最後の場合は各結果を格納します。

どれ疑い、私は

答えて

6

を教えてください私はあなたが欲しい疑う:

var results = doc.Descendants("Objective") 
       .Select(x => x.Elements("Exam") 
           .Select(exam => (double) exam.Attribute("Result")) 
           .ToList()) 
       .ToList(); 

あるいは客観IDが重要であるならば、あなたはDictionary<int, List<double>>検討する必要があります

var results = doc.Descendants("Objective") 
       .ToDictionary(x => (int) x.Attribute("ID"), 
           x => x.Elements("Exam") 
            .Select(y => (double) y.Attribute("Result")) 
            .ToList()); 

または、Lookup<int, double>

var results = doc.Descendants("Exam") 
       .ToLookup(x => (int) x.Parent.Attribute("ID"), 
          x => x.Select(y => (double) y.Attribute("Result")); 
+0

'。'ではなく '、'があるべきではありません。あなたの.ToDictionary行の最後に? –

+0

@PhilippSchmid:Yup - ちょうどタイプミス:) –

+2

思ったが、マスターの投稿を編集したくなかった。 –

関連する問題