IEnumerable <KeyValuePair>返回类型问题

Lan*_*ins 0 c# linq ienumerable types

我正在研究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;
}
Run Code Online (Sandbox Code Playgroud)

Aus*_*nen 5

你必须创建KeyValuePairs.

...
select new KeyValuePair(y.Attribute("id").Value, z.Value)
Run Code Online (Sandbox Code Playgroud)

  • @LanceCollins`密钥值对字典的任何好处 - 不是那么多.`IEnumerable`只是一个元素序列,所以你不会有恒定的访问时间,比如`IDictionary <string,string>`,所以我建议改变方法签名并返回这个类型 (2认同)