存储IEnumerable以外的XML值

时间:2012-08-25 04:54:07

标签: c# linq-to-xml

我将XML值存储在IEnumerable中,就像这样

   public static IEnumerable bindstate()
    {
        var states = from b in getdata().Descendants("state").SelectMany(state => state.Elements("text"))
        orderby (string)  b
        select  (string) b;

        return states;

    }

除了IEnumerable之外,还有其他方法可以存储值吗?

2 个答案:

答案 0 :(得分:1)

通过将值存储到其他类型的IEnumerable中,不确定您的意思。 我想如果你改变了

的返回类型
IEnumerable 

IEnumerable<string>

这会帮助你。

如果你想要我,你也可以返回List<string>并添加

return states.ToList(); 

到返回语句以返回List os字符串。

答案 1 :(得分:0)

是的,您可以将IEnumerable<T>“具体化”为:

  1. List<T>:使用IEnumerable<T>.ToList()return states.ToList();
  2. ReadOnlyCollection<T>(实现IList<T>接口):使用IEnumerable<T>.AsReadOnly()return states.AsReadOnly();。因为它来自它的名字,返回的列表是只读的。
  3. 在这两种情况下,bindstate方法的首选返回类型为IList<T>ICollection<T>