C# 存储IEnumerable以外的XML值
我将XML值存储在IEnumerable中,如下所示C# 存储IEnumerable以外的XML值,c#,linq-to-xml,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; re
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之外,还有其他存储值的方法吗?不确定将值存储到其他类型的IEnumerable中是什么意思。 我想如果你把退货类型从
IEnumerable
到
返回操作系统字符串列表。是的,您可以将IEnumerable
具体化为:
List
:使用IEnumerable.ToList()
:返回states.ToList()代码>
ReadOnlyCollection
(实现IList
接口):使用IEnumerable.AsReadOnly()
:返回状态.AsReadOnly()代码>。由于它来自其名称,因此返回的列表是只读的
在这两种情况下,
bindstate
方法的首选返回类型是IList
或ICollection
您的意思是像IEnumerable
?是的,我在使用IEnumerable时遇到了问题,所以除此之外还有其他建议吗?即使我使用了IEnumerable,即使它不起作用。您到底想要什么?您的查询已经是类型IEnumerable
,只需将返回类型更改为该类型。我的意思是,我们可以用其他哪些方式存储数据?
IEnumerable<string>
return states.ToList();