2

I was doing this first to return a particular set of nodes under the parent node that had id equal to 1 which worked fantastically.

IEnumerable<XElement> world1 = LevelData.root.
                               Elements("world").
                               Where(element => (int?)element.Attribute("id") == 1);

But i needed to do this multiple times and each to have there own instance so i thought to put them into a list but it doesn't even compile telling me error CS0266:

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.List>'. An explicit conversion exists (are you missing a cast?)

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++;
        }

So I tried adding the cast List<IEnumerable<XElement>> just before LevelData.root. but then i get a invalidcastexception. I'm at a brick wall on where to go. Any advice?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Russell Cargill
  • 270
  • 1
  • 6
  • 19

4 Answers4

5

The Where method doesn't return a List<>, it returns an IEnumerable<>, which is lazy-evaluated when it is enumerated.

If you want an actual List<> object, stick a .ToList() at the end, and that will do it.

David Yaw
  • 27,383
  • 4
  • 60
  • 93
4
var world1 = LevelData.root
                           .Elements("world")
                           .Where(element => (int?)element.Attribute("id") == 1)
                           .ToList();

You need to use ToList() to convert it from IEnumerable, because the IList interface supports more functionality than the IEnumerable interface.

Ryan Gates
  • 4,501
  • 6
  • 50
  • 90
2

You can convert an IEnumerable<T> to List<T> by calling ToList()

List<XElement> world1 = LevelData.root
    .Elements("world")
    .Where(element => (int?)element.Attribute("id") == 1)
    .ToList();
Khan
  • 17,904
  • 5
  • 47
  • 59
2

Your question is a bit confusing, you seem to have an array of List<IEnumerable<XElement>> and you are trying to assign a IEnumerable<XElement> to it.

If you just want a List of IEnumerable<XElement> this should work

List<IEnumerable<XElement>> World = new List<IEnumerable<XElement>>();
for (int i = 0; i < noofworlds; i++)
{
    World.Add(LevelData.Root.Elements("world").Where(element => (int?)element.Attribute("id") == i));//with id == to i
}

Otherwise tou are going to have to create a new list to add to the array and add the IEnumerable<XElement> to that list.

List<IEnumerable<XElement>>[] World = new List<IEnumerable<XElement>>[noofworlds];
for (int i = 0; i < noofworlds; i++)
{
    World[i] = new List<IEnumerable<XElement>> { (LevelData.Root.Elements("world").Where(element => (int?)element.Attribute("id") == i)) };//with id == to i
}
sa_ddam213
  • 42,848
  • 7
  • 101
  • 110