0

私は linq メソッドに取り組んでいますが、戻り値の型をメソッド シグネチャと一致させることができないようです。どんなガイダンスでも大歓迎です。

ありがとう!

private static IEnumerable<KeyValuePair<string, string>> RunQuery(XDocument doc)
{
    var data = from b in doc.Descendants("Company")
               where b.Attribute("name").Value == "CompanyA"
               from y in b.Descendants("Shirt")
               from z in y.Descendants("Size")
               select new
               {
                   color = y.Attribute("id").Value,
                   price = z.Value
               };

    return data;
}
4

2 に答える 2

1

クエリをよりソーターにすることができます。下位レベルまでのすべての子孫を含む xml ノード全体を解析するfrom y in b.Descendants("Shirt")ため、を削除したことに注意してください。DescendantsこのクエリはDictionary<string, string>、 を実装する を返すIEnumerable<KeyValuePair<string, string>>ため、メソッド シグネチャを変更する必要はありませんが、これを行うことを強くお勧めします。これは、クライアントが定数時間で辞書要素にアクセスできないためです。

return doc.Descendants("Company")
          .Where(node => node.Attribute("name").Value == "CompanyA")
          .Descendants("Size")
          .ToDictionary(node => node.Attribute("id").Value,
                        node => node.Value);
于 2013-03-31T20:27:59.067 に答える