【发布时间】:2013-02-11 21:33:24
【问题描述】:
我首先这样做是为了返回父节点下的一组特定节点,其 id 等于 1,效果非常好。
IEnumerable<XElement> world1 = LevelData.root.
Elements("world").
Where(element => (int?)element.Attribute("id") == 1);
但我需要多次执行此操作,并且每个都有自己的实例,所以我想将它们放入一个列表中,但它甚至没有编译并告诉我错误 CS0266:
无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“System.Collections.Generic.List>”。存在显式转换(您是否缺少演员表?)
List<IEnumerable<XElement>>[] World = new List<IEnumerable<XElement>>[noofworlds];
foreach (List<IEnumerable<XElement>> wn in World)
{
Debug.WriteLine("World: "+w);
//this will make a list of all the nodes (elements) within the world specified by the id tag
World[w] = LevelData.Root.Elements("world").Where(element => (int?)element.Attribute("id") == 1);//with id == to i
w++;
}
所以我尝试在 LevelData.root. 之前添加演员 List<IEnumerable<XElement>>
但后来我得到一个无效的异常。我在一块砖墙上去哪里。有什么建议吗?
【问题讨论】:
-
您的
World变量是IEnumerables 的XElement列表数组。我很确定你永远不想这样做。 -
你为什么不想这样做?有什么缺点?
标签: c# list casting ienumerable xelement