Getting information from IEnumerable

  • Thread starter Thread starter K Viltersten
  • Start date Start date
K

K Viltersten

At this point, i usually use a foreach-loop
to run through all the XElement's in my
IEnumerable<XElement>. I noticed, i'd like
to address the elements using brackets and
an index (or like a dictionary, with the
tag name as a key).

Is it easily doable? If so, how?
 
K said:
At this point, i usually use a foreach-loop
to run through all the XElement's in my
IEnumerable<XElement>. I noticed, i'd like
to address the elements using brackets and
an index (or like a dictionary, with the
tag name as a key).

Is it easily doable? If so, how?

Yes. Just stuff it in a List<XElement>. There
is a List<> constructor that takes an IEnumerable<>
as argument.

And if it comes from LINQ for XML there is also
ToList in that.

Arne
 
At this point, i usually use a foreach-loop
to run through all the XElement's in my
IEnumerable<XElement>. I noticed, i'd like
to address the elements using brackets and
an index (or like a dictionary, with the
tag name as a key).

Is it easily doable? If so, how?

If you just need to get an element at a specific index once, use
ElementAt() method (but you should be aware that it does a linear scan
unless the object it's called is actually an IList<T> - then it uses
the indexer). To find an element by name, use Elements() method which
takes an XName argument; if there is precisely one such element, use
First() method to get the first value from the IEnumerable returned by
Elements().
 
At this point, i usually use a foreach-loop
Yes. Just stuff it in a List<XElement>. There
is a List<> constructor that takes an IEnumerable<>
as argument.

And if it comes from LINQ for XML there is also
ToList in that.

Great info! Thanks to all!
 
At this point, i usually use a foreach-loop
Yes. Just stuff it in a List<XElement>. There
is a List<> constructor that takes an IEnumerable<>
as argument.

And if it comes from LINQ for XML there is also
ToList in that.

As far i can see, DotNet complains when i try to
shove the obtained IEnumerable<XElement> into a
List<XElement>... Are you sure it's possible with
no explicit casting involved?
 
K said:
As far i can see, DotNet complains when i try to
shove the obtained IEnumerable<XElement> into a
List<XElement>... Are you sure it's possible with
no explicit casting involved?

No casting.

But you do need to call the List<T> constructor that accepts an
IEnumerable<T>.

e.g.


List<T> theList = new List(theEnumerable);

and not

List<T> theList = theEnumerable;
 
K said:
As far i can see, DotNet complains when i try to
shove the obtained IEnumerable<XElement> into a
List<XElement>... Are you sure it's possible with
no explicit casting involved?

"stuff it in" = use it as argument for constructor

assignment does not really stuff much

Arne
 
No casting.

But you do need to call the List<T> constructor that accepts an
IEnumerable<T>.

e.g.

List<T> theList = new List(theEnumerable);

Or, a tiny bit shorter with LINQ:

var theList = theEnumerable.ToList();
 
Yes. Just stuff it in a List said:
"stuff it in" = use it as argument for constructor
assignment does not really stuff much

I see you, very correctly and clearly so,
mention the use of constructor, when talking
about stuffing in. I must have missed it.
Please accept my appology.

I though by stuffing in you ment:
int stuffee = 3;
double stuffer = stuffee;
Well, i'll be off correcting the error.
Thanks to all of you guys!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top