471,355 Members | 2,037 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,355 software developers and data experts.

Getting information from IEnumerable

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?

--
Regards
Konrad Viltersten
Jul 22 '08 #1
9 1976
On Tue, 22 Jul 2008 14:31:43 -0700, K Viltersten <tm**@viltersten.com>
wrote:
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?
When you write "my IEnumerable<XElement>" does that mean that you're the
author of the class that returns the IEnumerable?

If so, yes...you should easily be able to add an indexer. See:
http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx
Jul 22 '08 #2
K Viltersten wrote:
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
Jul 22 '08 #3
On Jul 23, 1:31*am, "K Viltersten" <t...@viltersten.comwrote:
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().
Jul 23 '08 #4
>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.
Great info! Thanks to all!

--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.
Jul 23 '08 #5
>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.
As far i can see, DotNet complains when i try to
shove the obtained IEnumerable<XElementinto a
List<XElement>... Are you sure it's possible with
no explicit casting involved?

--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.
Jul 23 '08 #6
K Viltersten wrote:
>>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.

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

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

e.g.
List<TtheList = new List(theEnumerable);

and not

List<TtheList = theEnumerable;
Jul 23 '08 #7
K Viltersten wrote:
>>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.

As far i can see, DotNet complains when i try to
shove the obtained IEnumerable<XElementinto 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
Jul 24 '08 #8
On Jul 23, 10:14*pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
As far i can see, DotNet complains when i try to
shove the obtained IEnumerable<XElementinto a
List<XElement>... Are you sure it's possible with
no explicit casting involved?

No casting.

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

e.g.

List<TtheList = new List(theEnumerable);
Or, a tiny bit shorter with LINQ:

var theList = theEnumerable.ToList();
Jul 24 '08 #9
>>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<XElementinto 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
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!

--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.
Jul 24 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by jcc | last post: by
3 posts views Thread by KH | last post: by
5 posts views Thread by Tin Gherdanarra | last post: by
2 posts views Thread by =?Utf-8?B?a2VubmV0aEBub3NwYW0ubm9zcGFt?= | last post: by
2 posts views Thread by Morgan Cheng | last post: by
4 posts views Thread by Ronald S. Cook | last post: by
1 post views Thread by =?Utf-8?B?UElFQkFMRA==?= | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.